JavaScript创建对象与继承
2012-10-22 13:34
435 查看
/** * 原型式继承 */ function object(o) { function F() {} F.prototype = o; return new F(); } /** * 寄生组合式继承 */ function inheritPrototype(subClass, superClass) { var prototype = object(superClass.prototype); prototype.constructor = subClass; subClass.prototype = prototype; } /** * 动态原型模式 创建Person对象 */ function Person(name) { this.name = name; if (typeof this.sayName !== "function") { Person.prototype.sayName = function() { alert("Hello " + this.name); } } } /** * 动态原型模式 创建Student对象 */ function Student(name, age) { Person.call(this, name); this.age = age; if (typeof this.sayHello !== "function") { Student.prototype.sayHello = function() { alert("My name is" + this.name + ", and my age is " + this.age); } } } /** * Student继承Person的方法 */ inheritPrototype(Student, Person); var student = new Student("Jobs", 20); student.sayName(); // Hello Jobs
相关文章推荐
- javascript创建对象、对象继承的实用方式详解
- 从0开始学react netive:2.Javascript创建对象的方式_prototype原型的概念_原型继承
- JAVASCRIPT 对象的创建和对6种继承模式的理解和遐想
- javascript的函数、创建对象、封装、属性和方法、继承
- JavaScript构造函数+原型创建对象,原型链+借用构造函数模式继承父类练习
- JavaScript创建对象及对象继承
- 【JavaScript】【学习】对象的创建和继承
- 在没有类的情况下,JavaScript如何创建对象、实现继承?
- javascript创建对象与继承
- 【JavaScript高级程序设计】对象的创建与继承
- JavaScript面向对象中的属性,对象创建,继承
- JavaScript对象的创建和继承实现
- 谈谈JavaScript里对象的创建和继承(一)
- javascript 创建对象与继承总结和回顾
- javascript的函数、创建对象、封装、属性和方法、继承
- JavaScript的中对象创建和继承原理
- javascript面向对象中的对象创建、继承、封装等实现方式
- javascript中的对象创建与继承
- javascript继承(二)—创建对象的三种模式
- JavaScript2种构造函数创建对象的模式以及继承的实现