实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Constructor Pattern Example</title>
5
    <script type="text/javascript">
6
    
7
        function Person(name, age, job){
8
            this.name = name;
9
            this.age = age;
10
            this.job = job;
11
            this.sayName = function(){
12
                alert(this.name);
13
            };    
14
        }
15
        
16
        var person1 = new Person("Nicholas", 29, "Software Engineer");
17
        var person2 = new Person("Greg", 27, "Doctor");
18
        
19
        person1.sayName();   //"Nicholas"
20
        person2.sayName();   //"Greg"
21
        
22
        alert(person1 instanceof Object);  //true
23
        alert(person1 instanceof Person);  //true
24
        alert(person2 instanceof Object);  //true
25
        alert(person2 instanceof Person);  //true
26
        
27
        alert(person1.constructor == Person);  //true
28
        alert(person2.constructor == Person);  //true
29
        
30
        alert(person1.sayName == person2.sayName);  //false        
31
        
32
        
33
    </script>
34
</head>
35
<body>
36
37
</body>
38
</html>