实例代码“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 = {
11
            constructor: Person,
12
            name : "Nicholas",
13
            age : 29,
14
            job : "Software Engineer",
15
            friends : ["Shelby", "Court"],
16
            sayName : function () {
17
                alert(this.name);
18
            }
19
        };
20
        
21
        var person1 = new Person();
22
        var person2 = new Person();
23
        
24
        person1.friends.push("Van");
25
        
26
        alert(person1.friends);    //"Shelby,Court,Van"
27
        alert(person2.friends);    //"Shelby,Court,Van"
28
        alert(person1.friends === person2.friends);  //true
29
30
        
31
    </script>
32
</head>
33
<body>
34
35
</body>
36
</html>