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