创建对象和继承
来源:互联网 发布:软件研发管理工具 编辑:程序博客网 时间:2024/05/16 11:39
1.创建对象
//工厂模式---无法识别对象,无法知道对象类型function createPerson(name, age) { var person = new Object(); person.name = name; person.age = age; person.sayName = function () { alert(this.name); } return person;}//构造函数模式---sayName是不同对象的实例(抽出来作为全局函数,但是破坏封装性)function Person(name, age) { this.name = name; this.age = age; this.sayName = function () { alert(this.name); }}function prototype() { //原型模式---引用类型的数据会出错 function Person() { } Person.prototype = { constructor: Person,//重写默认的constructor使其指向Person name: "json", age: 23, sayName: function () { alert(this.name); } }; var p1 = new Person(); p1.sayName();}//构造函数模式和原型模式混合---最常用function standard() { function Person(name, age) { this.name = name; this.age = age; this.friends = ["kitty", "jack"]; } Person.prototype = { constructor: Person,//重写默认的constructor使其指向Person sayName: function () { alert(this.name); } }; var p1 = new Person("jack", 11); var p2 = new Person("rose", 22); p1.friends.push("hhh"); alert(p1.friends); alert(p2.friends);}
2.继承
//原型链:利用原型让一个引用类型继承另一个引用类型的属性和方法---引用类型会出问题function prototype() { function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = function () { return this.property; } function SubType() { this.subproperty = false; } SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function () { return this.subproperty; } var instance = new SubType(); alert(instance.getSuperValue()); alert(instance.getSubValue());}//借用构造函数---优势:子类向超类传参;劣势:没法函数复用function constructureStealing() { function SuperType() { this.colors = ["red", "blue"]; } function SubType() { SuperType.call(this); } var instance = new SubType(); instance.colors.push("green"); alert(instance.colors); var instance2 = new SubType(); alert(instance2.colors);}//原型链实现属性和方法的继承,借用构造函数实现实例属性的继承function mix() { function SuperType(name) { this.name = name; this.colors = ["red", "blue"]; } SuperType.prototype.sayName = function () { alert(this.name); } function SubType(name, age) { //继承属性 SuperType.call(this, name); this.age = age; } //继承方法 SubType.prototype = new SuperType(); SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function () { alert(this.age); } var instance = new SubType("jack", 29); instance.colors.push("green"); alert(instance.colors); instance.sayName(); instance.sayAge(); var instance2 = new SubType("rose", 20); alert(instance2.colors); instance2.sayName(); instance2.sayAge();}
阅读全文
0 0
- 创建对象和继承
- js创建对象和继承
- JS如何创建对象和继承对象
- JS对象的创建和继承
- JavaScript中怎样创建对象和继承
- lua类对象创建继承和使用
- js对象的创建和继承
- 基于原型的对象创建和继承
- JavaScript对象的创建和继承实现
- javascript对象的创建和继承实现
- js 对象深复制,创建对象和继承
- js 对象深复制,创建对象和继承
- 创建对象及继承
- js创建对象继承
- JavaScript的中对象创建和继承原理
- 关于prototype Chain创建对象和实现继承
- JavaScript之对象实例的创建和继承
- lua类对象创建继承和使用2
- CSS框模型
- eclipse的安装
- 拖拽功能
- jQuery 9 AJAX 方法
- 《未来简史》作者访谈观点摘要
- 创建对象和继承
- #7 Loading/Error 子状态
- SpringBoot JavaMailSender发送邮件(QQ和163邮箱)
- 机器学习实战:朴素贝叶斯分类(一)
- Java中List集合的遍历
- CSS中的绝对定位与相对定位
- Google Chrome v59.0.3071.104
- CSS 绝对定位
- k数和