js创建对象和继承

来源:互联网 发布:中国白银集团淘宝店 编辑:程序博客网 时间:2024/05/17 22:06

一、创建对象

1、组合模式(构造函数模式和原型模式):构造函数用于定义实例属性,而原型模式用于定义方法和共享属性。

function Person(name ,age,job){    this.name = name;    this.age = age;    this.job = job;    this.friends = ['a','b','c'];}Person.prototype = {    constructor : Person,    sayName : function(){        alert(this.name);    }}; var person1 = new Person("Nicholas", 29, "Software Engineer");        var person2 = new Person("Greg", 27, "Doctor");                person1.friends.push("Van");                alert(person1.friends);    //"Shelby,Court,Van"        alert(person2.friends);    //"Shelby,Court"        alert(person1.friends === person2.friends);  //false        alert(person1.sayName === person2.sayName);  //true

2、动态原型模式

function Person(name,age,job){    this.name =name;     this.age = age;    this.job = job;    if(typeof this.sayName != "function"){        Person.prototype.sayName = function (){            alert(this.name);        };    }var person1 = new Person("nic","20","soft");

二、继承(js主要通过原型链实现继承)

1、组合继承:使用原型链实现对原型属性和方法的继承,而通过构造函数实现实例属性的继承

 function SuperType(name){            this.name = name;            this.colors = ["red", "blue", "green"];        }                SuperType.prototype.sayName = function(){            alert(this.name);        };        function SubType(name, age){              SuperType.call(this, name);  ////第二次调用SuperType()                        this.age = age;        }        SubType.prototype = new SuperType(); //第一次调用SuperType()        SubType.prototype.constructor = SubType;        SubType.prototype.sayAge = function(){            alert(this.age);        };                var instance1 = new SubType("Nicholas", 29);        instance1.colors.push("black");        alert(instance1.colors);  //"red,blue,green,black"        instance1.sayName();      //"Nicholas";        instance1.sayAge();       //29                       var instance2 = new SubType("Greg", 27);        alert(instance2.colors);  //"red,blue,green"        instance2.sayName();      //"Greg";        instance2.sayAge();       //27

2、寄生组合式继承:不必为了指定子类型的原型而调用超类型的构造函数,所需要的无非就是超类型原型的一个副本。

function object(o){            function F(){}            F.prototype = o;            return new F();        }            function inheritPrototype(subType, superType){            var prototype = object(superType.prototype);   //create object            prototype.constructor = subType;               //augment object            subType.prototype = prototype;                 //assign object        }                                        function SuperType(name){            this.name = name;            this.colors = ["red", "blue", "green"];        }                SuperType.prototype.sayName = function(){            alert(this.name);        };        function SubType(name, age){              SuperType.call(this, name);                        this.age = age;        }        inheritPrototype(SubType, SuperType);                SubType.prototype.sayAge = function(){            alert(this.age);        };                var instance1 = new SubType("Nicholas", 29);        instance1.colors.push("black");        alert(instance1.colors);  //"red,blue,green,black"        instance1.sayName();      //"Nicholas";        instance1.sayAge();       //29                       var instance2 = new SubType("Greg", 27);        alert(instance2.colors);  //"red,blue,green"        instance2.sayName();      //"Greg";        instance2.sayAge();       //27


0 0