实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Constructor Pattern Example 2</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 person = new Person("Nicholas", 29, "Software Engineer");
17
        person.sayName();   //"Nicholas"
18
        
19
        Person("Greg", 27, "Doctor");  //adds to window
20
        window.sayName();   //"Greg"
21
        
22
        var o = new Object();
23
        Person.call(o, "Kristen", 25, "Nurse");
24
        o.sayName();    //"Kristen"
25
        
26
    </script>
27
</head>
28
<body>
29
30
</body>
31
</html>