实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Prototype Pattern Example 2</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
        var person2 = new Person();
19
        
20
        person1.name = "Greg";
21
        alert(person1.name);   //"Greg" from instance
22
        alert(person2.name);   //"Nicholas" ?from prototype
23
24
    </script>
25
</head>
26
<body>
27
28
</body>
29
</html>