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
- js创建对象和继承
- JS如何创建对象和继承对象
- JS对象的创建和继承
- js对象的创建和继承
- js创建对象继承
- js 对象深复制,创建对象和继承
- js 对象深复制,创建对象和继承
- 创建对象和继承
- js(十六)——原型创建对象和继承
- js中创建对象和继承的几种方式
- 理解js的对象,对象创建,继承
- js面向对象和继承
- 50 JS-4 创建对象 继承
- JS如何创建对象及实现继承
- js加强:原型,创建对象分析,继承
- js 创建对象与原型链继承
- JS创建对象与继承总结
- JS创建对象方法以及继承方法
- C# devExpress GridControl 行中行 子行 多级行
- poj 2151 Check the difficulty of problems(概率dp)
- 手势识别器
- linux常用c函数字符串转换篇
- csdn还是"待审核"吗?
- js创建对象和继承
- POJ 1279 Art Gallery
- Java中I/O流
- mysql connector odbc配置注意事项
- 解释器模式
- 浏览器嵌入视频播放器(转载)
- 嵌入式linux多进程编程
- PHP网页游戏学习之Xnova(ogame)源码解读
- C 语言堆栈的实例