javascript 动态原型模式

<html>
<head>
    <title>Dynamic Prototype Pattern Example</title>
    <script type="text/javascript">
                    
        function Person(name, age, job){
        
            //properties
            this.name = name;
            this.age = age;
            this.job = job;
            
            //methods
            if (typeof this.sayName != "function"){
            
                Person.prototype.sayName = function(){
                    alert(this.name);
                };
                
            }
        }

        var person = new Person("Nicholas", 29, "Software Engineer");
        person.sayName();
      
    </script>
</head>
<body>

</body>
</html>
posted @ 2012-06-05 09:43  WFKeypoints  阅读(125)  评论(0编辑  收藏  举报