实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Hybrid 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.friends = ["Shelby", "Court"];
12
        }
13
        
14
        Person.prototype = {
15
            constructor: Person,
16
            sayName : function () {
17
                alert(this.name);
18
            }
19
        };
20
        
21
        var person1 = new Person("Nicholas", 29, "Software Engineer");
22
        var person2 = new Person("Greg", 27, "Doctor");
23
        
24
        person1.friends.push("Van");
25
        
26
        alert(person1.friends);    //"Shelby,Court,Van"
27
        alert(person2.friends);    //"Shelby,Court"
28
        alert(person1.friends === person2.friends);  //false
29
        alert(person1.sayName === person2.sayName);  //true
30
31
        
32
    </script>
33
</head>
34
<body>
35
36
</body>
37
</html>