先上代码说明
KevLinDev.extend = function(subClass, baseClass) {
function inheritance() {}
inheritance.prototype = baseClass.prototype;
subClass.prototype = new inheritance();
subClass.prototype.constructor = subClass;
subClass.baseConstructor = baseClass;
subClass.superClass = baseClass.prototype;
}
function Employee(first, last, id) {
// initialize properties here
}
KevLinDev.extend(Employee, Person);
1、We need the subClass.prototype property to be equivalent to baseClass.prototype. But we won't write like this:
subClass.prototype = baseClass.prototype
cause we can't allow both the subClass.prototype and the baseClass.prototype to point to the same prototype object. That would mean adding methods to one class would add it to the other. So that's why we define a nested function called "inheritance". Remember, when we call "new" that prototype property will be copied to the new instance's private prototype, thus hooking up its inheritance chain.
2、we create a new instance of "inheritance" and assign that to the subClass prototype. Now when we create a new instance of subClass, the instance's private prototype will point to the "inheritance" instance. The "inheritance" instance's private prototype points to the base class' public prototype. This means that changes to baseClass.prototype will propagate to subclass instances through the inheritance chain and since we created a new object for the subclass' prototype property, we can add to subClass.prototype without affecting the base class prototype.
3、Whenever you create a new object instance in JavaScript, the instance's local "constructor" property points to the function (constructor) that was invoked to create the instance. I often compare an instance's constructor property against a function to test the "type" of an object instance. Calling "new inheritance()" points subClass.prototype.constructor to the nested "inheritance" function which would break my tests. I fix this by updating the constructor property to point to my subClass constructor.
4、The final two lines are used as convenience properties when invoking ancestor constructors and methods.
How to use:
function Person(first, last) {
this.first = first;
this.last = last;
}
Person.prototype.toString = function() {
return this.first + " " + this.last;
};
function Employee(first, last, id) {
Employee.baseConstructor.call(this, first, last);
this.id = id;
}
KevLinDev.extend(Employee, Person);
// var emp = new Employee('ding','wei','01');
// alert(emp.toString()); 此时emp通过prototype chain,toString引用的是Person里定义的,所以当然不会输出id的值
// output ding wei
Employee.prototype.toString = function() {
return Employee.superClass.toString.call(this) + ": " + this.id;
};
// 经过这一步骤(可以理解为覆写了toString方法),再执行toString时,就会输出id值了
// output ding wei : 01
// 回想下extend里那个内嵌函数inheritance的定义,如果我们直接subClass.prototype = baseClass.prototype,再执行person.toString(),看看会有什么变化
function Manager(first, last, id, department) {
Manager.baseConstructor.call(this, first, last, id);
this.department = department;
}
KevLinDev.extend(Manager, Employee);
Manager.prototype.toString = function() {
return Manager.superClass.toString.call(this) + ": " + this.department;
};
总结一下:
1、想把代码的英文说明翻译过来,但总觉得不到位,还是暂时看英文的来得明白
2、extend所接受的参数是什么?不要被subClass等名字搞混了,JS里没有类的概念,而是通过function来模拟类的定义,因此这里传的参数实际就是函数引用
3、function中公开的prototype属性是什么?实际上它的值就是个普通的对象引用。那这个对象有什么属性呢?这么说吧,Person.prototype.toString1 = function(){}; Person.prototype.toString2 = function(){};那么Person的prototype就具有toString1和toString2两个属性。
4、extend方法主要就是搭起prototype这条链。把Person中prototype的每个属性拷贝到Employee的prototype里,这样当创建Employee实例时,这些实例就能访问到定义在Person中prototype里的属性。具体参见JavaScript对象模型
原文Kevin Lindsey的inheritance prototye,参考sp42的JavaScript“类”继承横向比较
分享到:
相关推荐
John Resig的"Simple JavaScript Inheritance"提供了一种简洁的方法来实现这种继承模式,它受到了base2和PrototypeJS库的启发。 在Resig的实现中,关键在于如何处理`_super`,这是一个用来引用父类方法的关键字。...
This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...
JavaScript,作为一种广泛应用于Web开发的脚本语言,其高级特性如闭包(closures)、原型(prototype)和继承(inheritance)是理解其精髓的关键。本文将深入探讨这些概念,帮助开发者更好地掌握JavaScript的核心。 ...
更不用谈继承、多态了,为了模拟出一些其它面向对象编程语言的这些特性,有好多大牛写了给出了实现方式,看了John Resig的《Simple JavaScript Inheritance》这篇文章,深深被折服了,原来短短几十行javascript也...
##Augment 简介The world's smallest and fastest classical JavaScript inheritance pattern, augment, is a seven line function which allows you to write CoffeeScript style classes with a flair of ...
* Learn the options available for code reuse and inheritance in JavaScript * Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more * Examine ...
Zakas thoroughly explores JavaScript's object-oriented nature, revealing the language's unique implementation of inheritance and other key characteristics. You'll learn: –The difference between ...
Reuse code with common patterns for inheritance Make your programs cleaner, faster and compatible with other programs and libraries Use object-oriented JavaScript for improving script performance ...
5. **继承(Inheritance)**:JavaScript通过原型链实现继承。子类的原型会指向父类的实例,从而继承其属性和方法。ES6的`class`语法也支持`extends`关键字进行继承。 ```javascript class Employee extends ...
Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, ...
在JavaScript中,面向对象编程是实现复杂功能和代码复用的关键。继承是面向对象的核心特性之一,它允许一个对象(子类)获取另一个对象(父类)的属性和方法。本篇文章将深入探讨JavaScript实现继承的七种常见方式,...
Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...