Please see source code:
Copy Code code as follows:
function Clone (o) {
var F = function () {};
F.prototype = O;
return new F ();
}
First Look at Ext (4.1 of 1896 lines start) of the prototype inheritance.
Copy Code code as follows:
var templateclass = function () {};
var Extobject = Ext.object = {
Chain:function (object) {
Templateclass.prototype = object;
var result = new Templateclass ();
Templateclass.prototype = null;
return result;
}
}
This clears the prototype of object.
And look at how jquery is playing with inheritance.
Copy Code code as follows:
var jQuery = function (selector, context) {
Return to New JQuery.fn.init (selector, context, rootjquery);
};
-----------------------
Jquery.fn = Jquery.prototype = {
Constructor:jquery,
Init:function (selector, context, rootjquery) {
-----------------------
}
}
-------------------
JQuery.fn.init.prototype = Jquery.fn;
jquery play is relatively high, with the help of JQuery.fn.init to complete, but the idea is the same.
Masaki's mass also had similar inheritance in the 17th line of Lang_fix.js:
Copy Code code as follows:
Create:function (o) {
if (Arguments.length > 1) {
$.log ("Object.create implementation only accepts")
}
function F () {}
F.prototype = O;
return new F ();
}
Check out ES5 's official and found his compatible patch:
Copy Code code as follows:
ES5 15.2.3.5
http://es5.github.com/#x15.2.3.5
if (! Object.create) {
Object.create = function Create (prototype, properties) {
var object;
if (prototype = = NULL) {
Object = {"__proto__": null};
} else {
if (typeof prototype!= "Object") {
throw new TypeError ("typeof prototype[" + (typeof prototype) + "]!= ' object '");
}
var Type = function () {};
Type.prototype = prototype;
Object = new Type ();
IE has no built-in implementation of ' object.getprototypeof '
Neither ' __proto__ ', but this manually setting ' __proto__ ' would
Guarantee that ' object.getprototypeof ' would work as expected with
Objects created using ' Object.create '
object.__proto__ = prototype;
}
if (properties!== void 0) {
Object.defineproperties (object, properties);
}
return object;
};
}
The above code to consider a more comprehensive, but need to introduce a object.defineproperties patch to the line, the source is relatively much more.
Copy Code code as follows:
ES5 15.2.3.6
http://es5.github.com/#x15.2.3.6
Patch for WebKit and IE8 Standard mode
Designed by Hax Related ISSUE:HTTPS://GITHUB.COM/KRISKOWAL/ES5-SHIM/ISSUES#ISSUE/5
IE8 Reference:
Http://msdn.microsoft.com/en-us/library/dd282900.aspx
Http://msdn.microsoft.com/en-us/library/dd229916.aspx
WebKit Bugs:
https://bugs.webkit.org/show_bug.cgi?id=36423
function Doesdefinepropertywork (object) {
try {
Object.defineproperty (Object, "Sentinel", {});
Return "Sentinel" in object;
} catch (Exception) {
Returns Falsy
}
}
Check whether DefineProperty works if it ' s given. Otherwise,
Shim partially.
if (Object.defineproperty) {
var definepropertyworksonobject = doesdefinepropertywork ({});
var definepropertyworksondom = typeof document = = "Undefined" | |
Doesdefinepropertywork (document.createelement ("div"));
if (!definepropertyworksonobject | |!definepropertyworksondom) {
var definepropertyfallback = Object.defineproperty;
}
}
if (! Object.defineproperty | | Definepropertyfallback) {
var err_non_object_descriptor = "Property Description must is an OBJECT:";
var err_non_object_target = "Object.defineproperty called on Non-object:"
var err_accessors_not_supported = "Getters & setters can not be defined" +
"On this JavaScript engine";
Object.defineproperty = function DefineProperty (object, property, descriptor) {
if ((TypeOf object!= "Object" && typeof object!= "function") | | | object = = NULL) {
throw new TypeError (Err_non_object_target + OBJECT);
}
if ((typeof descriptor!= "Object" && typeof descriptor!= "function") | | descriptor = = NULL) {
throw new TypeError (Err_non_object_descriptor + descriptor);
}
Make a valiant attempt to with the real DefineProperty
For I8 ' s DOM elements.
if (definepropertyfallback) {
try {
Return Definepropertyfallback.call (Object, object, property, descriptor);
} catch (Exception) {
Try the shim if the real one doesn ' t work
}
}
If it ' s a data property.
if (Owns (descriptor, "value")) {
Fail silently if "writable", "enumerable", or "configurable"
are requested but not supported
/*
Alternate approach:
if (//can ' t implement features allow false but not true
! (Owns (descriptor, "writable")? descriptor.writable:true) | |
! (Owns (descriptor, "enumerable")? descriptor.enumerable:true) | |
! (Owns (descriptor, "configurable")? descriptor.configurable:true)
)
throw New Rangeerror (
"This implementation of Object.defineproperty does" +
"Support configurable, enumerable, or writable."
);
*/
if (supportsaccessors && lookupgetter (Object, property) | |
Lookupsetter (object, property))
{
As accessors are supported only on engines implementing
' __proto__ ' We can safely override ' __proto__ ' while defining
A property to make sure so we don ' t hit an inherited
Accessor.
var prototype = object.__proto__;
object.__proto__ = Prototypeofobject;
Deleting a property anyway since Getter/setter may
defined on object itself.
Delete Object[property];
Object[property] = Descriptor.value;
Setting original ' __proto__ ' back now.
object.__proto__ = prototype;
} else {
Object[property] = Descriptor.value;
}
} else {
if (!supportsaccessors) {
throw new TypeError (err_accessors_not_supported);
}
If we got that far then getters and setters can be defined!!
if (Owns (descriptor, "get")) {
Definegetter (Object, property, Descriptor.get);
}
if (Owns (descriptor, "set")) {
Definesetter (Object, property, Descriptor.set);
}
}
return object;
};
}
ES5 15.2.3.7
http://es5.github.com/#x15.2.3.7
if (! Object.defineproperties) {
Object.defineproperties = function Defineproperties (object, properties) {
For (Var property in properties) {
if (owns (properties) && property!= "__proto__") {
Object.defineproperty (Object, property, Properties[property]);
}
}
return object;
};
}
EcmaScript6 class inheritance.
Copy Code code as follows:
class Module extends Base {
Constructor () {
}
}
More and more like Java, but es6 many browsers do not support.
The final recommended wording:
Copy Code code as follows:
if (! Object.create) {
Object.create = function Create (o) {
var F = function () {};
F.prototype = O;
var result = new F ();
F.prototype = null;
return result;
}
}