码迷,mamicode.com
首页 > Web开发 > 详细

js常用设计模式

时间:2015-10-28 22:26:07      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

组合使用构造函数模式和原型模式。其中,构造函数模式用于定义实例属性,而原型模式用于定义方法和共享属性。

例子:

    <script>
        function Person(name,age,job){
            this.name = name;
            this.age = age;
            this.job = job;
            this.friends = ["jie","fei"];

        }

//        Person.prototype = {
//            constructor : Person,
//            sayName : function(){
//                alert(this.name);
//            }
//        }
        Person.prototype.sayName = function(){
            alert(this.name);
        }
        var  person1 = new  Person("jie",24,"web");
        var person2 = new Person("fei",24,"teacher");

        person1.friends.push("wang");
        alert(person1.friends);
        alert(person2.friends);
        alert(person1.friends == person2.friends);
        alert(person1.sayName == person2.sayName);
    </script>

 

js常用设计模式

标签:

原文地址:http://www.cnblogs.com/scnuwangjie/p/4918577.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!