请看源码:
复制代码 代码如下:
function clone(o) {
var F = function(){};
F.prototype = o;
return new F();
}
首先看ext(4.1的1896行开始)的原型式继承。
复制代码 代码如下:
var TemplateClass = function(){};
var ExtObject = Ext.Object = {
chain: function (object) {
TemplateClass.prototype = object;
var result = new TemplateClass();
TemplateClass.prototype = null;
return result;
}
}
这里清除了object的prototype。
再看一下jquery是怎么玩的继承。
复制代码 代码如下:
var jQuery = function( selector, context ) {
return 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玩的就比较高,借助jQuery.fn.init来完成,但是思路一样。
司徒正美的mass里也有类似的继承,在lang_fix.js里面第17行:
复制代码 代码如下:
create: function(o){
if (arguments.length > 1) {
$.log(" Object.create implementation only accepts the first parameter.")
}
function F() {}
F.prototype = o;
return new F();
}
查看了一下es5的官方,找到了他的兼容补丁:
复制代码 代码如下:
// 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__` will
// guarantee that `Object.getPrototypeOf` will work as expected with
// objects created using `Object.create`
object.__proto__ = prototype;
}
if (properties !== void 0) {
Object.defineProperties(object, properties);
}
return object;
};
}
上面的代码考虑的就比较全面,但是需要另外引入Object.defineProperties的补丁才行,源码相对就比较多了。
复制代码 代码如下:
// ES5 15.2.3.6
// http://es5.github.com/#x15.2.3.6
// Patch for WebKit and IE8 standard mode
// Designed by hax <hax.github.com>
// 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 be 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 use 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 these 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 not " +
"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 that we don't hit an inherited
// accessor.
var prototype = object.__proto__;
object.__proto__ = prototypeOfObject;
// Deleting a property anyway since getter / setter may be
// 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) && property != "__proto__") {
Object.defineProperty(object, property, properties[property]);
}
}
return object;
};
}
EcmaScript6的类继承。
复制代码 代码如下:
class module extends Base {
constructor() {
}
}
越玩越像java了,不过es6很多浏览器还不支持。
最后推荐的写法:
复制代码 代码如下:
if (!Object.create) {
Object.create = function create(o) {
var F = function(){};
F.prototype = o;
var result = new F();
F.prototype = null;
return result;
}
}
相关推荐:
什么是蜘蛛弛?揭秘这个SEO优化背后的神秘工具,南沙seo优化排名价格
SEO做法-提升网站流量与排名的关键秘诀,屏东网站推广招聘
用AI修改文章,提升写作效率与质量的新时代
AI人工智能生成文章:开启写作新时代
Chatttst:开启智能沟通新时代的无限可能,上海联通ai
ChatGPT空白对话:释放创意,开启智能对话的新世界,ai重庆南坪
企业SEO:如何通过搜索引擎优化提升企业网络竞争力,安阳网站优化布局设计
SEO中的别类词:提升排名,轻松超越竞争对手,ai插画生肖
SEO经营:助力企业腾飞的秘密武器,靖边百度关键词排名
优化原理:提升效率、创新突破的核心法则,印刷包装推广有哪里网站
SEO深度解析:如何通过深度优化提升网站排名,带来流量和转化,咸宁网站建设大概费用
SEO排位:如何通过精准策略提升网站排名,获得流量与转化,林海网络推广营销
ChatGPT-01:开创人工智能新纪元,AI如何画模糊的云层
ChatGPT页面怎么拖不动?解决问题的终极指南,日韩AI换脸在线观看
SEO获取流量的必杀技:如何通过优化轻松提升网站排名,德州全网营销推广价格
SEO优化工具优势:提升网站排名,带来流量和转化的秘密武器,ai圆形造字
企业如何借助SEO咨询实现精准流量引爆,助力业绩提升,立刻推广的旅游线下营销
ChatGPT无法加载?检查网络并尝试重启,助您快速恢复畅通体验,burj ai
SEO用户:如何为您的网站带来持续流量和转化,惠州网站推广哪个好
智能AI写作生成:如何借助人工智能提升创作效率与质量
seo跟sem是什么,seo和sem的概念 ,惠威的ai功能
SEO查:如何用精准的SEO诊断助力网站流量爆发,美团关键词排名怎么补
SEO优化攻略:如何通过精准策略提升网站排名与流量,aI ow翻译
SEO获客的秘诀:如何通过搜索引擎优化提升客户获取能力,厦门seo搜索优化排名
SEO收集:如何通过有效数据收集提升网站排名,鞍山商城网站建设报价
未来智能:AI智能人工软件引领数字化变革
ChatGPT打开后空白:如何解决这个困扰并高效使用AI助手,ai四声怎么写
ChatGPT4账号共享-让AI助力你的学习与工作,轻松提升效率,ai巨无霸
目前最火的AI软件有哪些?深度解析必备工具
ChatGPT出现错误503?你需要知道的解决方案和应对策略,ai拉伸字效
ChatGPT出问题?背后的原因与解决方案,夸克ai写作生成
SEO关键词的选择步骤:让你的网站在搜索引擎中脱颖而出,ai书法化
人工AI软件的未来:智能时代的创新驱动力
ChatGPT的梯子:突破网络壁垒,畅享智能对话的全新体验,ai的音标1001ai的音标
SEO部:开启数字化营销新纪元的幕后英雄,株洲营销推广是什么公司
为什么越来越多的人选择在知乎发布文章?这里面的机会你可能还没发现!,潍坊关键词排名提升
SEO教你如何快速提升网站排名,打破竞争壁垒!,本溪seo优化排名公司
ChatGPT:人工智能对话新时代的领航者,ai音响华为还是小米好
AI一键生成原创文章,让创作更高效更轻松!
优化提长:让企业效能提升的秘密武器,长沙网站建设创意
什么是seo伪原创,seo就业前景伪原创怎么写 ,头像ai画怎么弄
ChatGPT+维护页面:您的智能助手之旅,安全、高效、无忧,奥特曼画图ai
SEO专业怎么样?未来发展的无限潜力与职业前景,联通ai智能早教
AI写作免费一键生成5000字:高效创作的革命性工具
SEO运营是什么职业,seo运营工程师招聘 ,把ai图层改横幅
ChatGPT的破解版:AI世界的新突破,ai无禁忌
AI生成网页模板,轻松打造专业网站,ai网格画法
文章生成AI:让写作轻松高效的神奇工具
丹东抖音seo是什么,抖音seo引流 ,ai工具编写作业指导书
seo要什么条件,seo都需要做什么 ,交互ai求职