Skip to content

Instantly share code, notes, and snippets.

@yangfch3
Created October 25, 2016 08:03
Show Gist options
  • Select an option

  • Save yangfch3/cb08ce4d1dfb7fbcfcebd8c576357dae to your computer and use it in GitHub Desktop.

Select an option

Save yangfch3/cb08ce4d1dfb7fbcfcebd8c576357dae to your computer and use it in GitHub Desktop.
JavaScript 继承最佳实践(寄生组合式继承)
function inherit (SubType, SuperType) {
if (Object.create) {
var prototype = Object.create(SuperType.prototype);
prototype.constructor = SubType;
SubType.prototype = prototype;
return;
}
function F() {}
F.prototype = SuperType.prototype;
var prototype = new F();
prototype.constructor = SubType;
SubType.prototype = prototype;
};
/**
* SuperType(){}
*
* SubType(){
* SuperType.call(this);
* ...
* }
*
* inherit(SubType, SuperType);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment