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