国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

淺談javascript的原型繼承

2024-05-06 14:21:29
字體:
來源:轉載
供稿:網友
請看源碼:
代碼如下:
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) {
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 德保县| 恩平市| 星座| 永川市| 宝应县| 兴安盟| 称多县| 宜昌市| 合阳县| 伊春市| 新野县| 丹巴县| 家居| 彭山县| 通化市| 永川市| 宁城县| 石家庄市| 丹东市| 靖州| 余姚市| 平湖市| 韩城市| 望都县| 安义县| 剑河县| 清水县| 阿拉尔市| 平顺县| 崇州市| 康平县| 开化县| 尼木县| 台南市| 满洲里市| 南涧| 武川县| 汉中市| 枣阳市| 萨迦县| 龙陵县|