淺談javascript的原型繼承_javascript技巧
請看原始碼:
複製程式碼 程式碼如下:
function clone(o) { var F = function(){};
F.prototype = o;
return new F();
}
複製程式碼 程式碼如下:
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是怎麼玩的繼承。
chain: function (object) {
TemplateClass.prototype = object;
var result = new TemplateClass();
TemplateClass.prototype = null; return result;
}
}
這裡清除了object的prototype。
再看一下jquery是怎麼玩的繼承。
複製程式碼 程式碼如下: var jQuery = function( 🎜>
var jQuery = function( 🎜>
var jQuery = function( ? >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 ;
var jQuery = function( ? >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) {
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__ / // objects created using `Object.create` object.__proto__ = prototype; } object.__proto__ = prototype; // ES5 15.2.3.6
// http://es5.github.com/#x15.2.3.6
// WebKit 和IE8 標準模式修補程式
//由hax
// 相關問題:https://github.com/kriskowal/es5-shim/issues#issue/5
// IE8 參考:
// http ://msdn.microsoft.com/ en-us/library/dd282900.aspx
// http://msdn.microsoft.com/en-us/library/dd229916.aspx
// WebKit 錯誤:
// https:// bugs.webkit.org/show_bug.cgi?id=36423
function doesDefinePropertyWork(object) {
try {
Object.defineProperty(object, "sentinel", {sen. });
傳回物件中的「哨兵」;
} catch (異常) {
// 回傳falsy
}
}
// 如果給定,檢查DefineProperty 是否有效。否則,
//部分填充。
if (Object.defineProperty) {
var DefinePropertyWorksOnObject = doesDefinePropertyWork({});
var DefinePropertyWorksOnDom = 文件類型==「未定義」||
doesDefinePropertyWork(document." ));
if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
var DefinePropertyFallback = Object.defineProperty;
}
}
if (!Object.defineProperty || || >var ERR_NON_OBJECT_DESCRIPTOR = "屬性描述必須是一個物件:";
var ERR_NON_OBJECT_TARGET = "在非物件上呼叫Object.defineProperty:"
var ERR_ACCESSORS_NOT_SUPORTED = "JAPORTEDPPORTED"好在此定義。 🎜>";
Object.defineProperty = function DefineProperty(物件, 屬性, 描述子) {
if ((typeof object != "object" && typeof object != "function") || object === null) {
拋出新的TypeError(ERR_NON_OBJECT_TARGET物件);
}
if ((描述符類型!= "物件" && 描述子類型!= "函數") || 描述符== = null) {
拋出新的TypeError(ERR_NON_OBJECT_DESCRIPTOR 描述詞);
}
// 大膽嘗試對I8 的DOM 元素使用真正的DefineProperty
try {
return DefinePropertyFallback.call(物件, 物件, 屬性, 描述子);
} catch (異常) {
// 如果真正的墊片不起作用,請嘗試墊片
}
}
// 如果它是資料屬性。
if (owns(descriptor, "value")) {
// 如果請求“可寫”、“可枚舉”或“可配置”,則靜默失敗
// 被請求但不支援
/*
// 替代方法:
if ( // 無法實作這些功能;允許false 但不允許true
!(owns(descriptor, "writable") ? detector.writable : true) ||
!(owns(descriptor, "enumerable") ?descriptor.enumerable : true) ||
!(owns(descriptor, "configurable") ?descriptor.configurable : true)
)
拋出新的RangeError(
"Object.defineProperty 的此實作不"
"支援可設定、可枚舉或可寫。"
);
*/
if (supportsAccessors && (lookupGetter(object, property) ||
lookupSetter(object, property)))
{
// 因為訪問器僅在實作//`` __proto__` 我們可以在定義
// 屬性時安全地重寫`__proto__`,以確保我們不會碰到繼承的
// 訪問器。
var 原型 = object.__proto__;
object.__proto__=prototypeOfObject;
// 無論如何都要刪除屬性,因為 getter / setter 可能是
// 在物件本身上定義的。
刪除物件[屬性];
物件[屬性] = 描述符.值;
// 現在設定原來的`__proto__`。
object.__proto__ = 原型;
} else {
物件[屬性] = 描述子.值;
}
} else {
if (!supportsAccessors) {
} else {
if (!supportsAccessors) {
}拋出新的TypeError(ERR_ACCESSORS_NOT_SUPPORTED);
}
// 如果我們做到了這一點,那麼就可以定義getter 和setter 了!
if (owns(descriptor, "get")) {
defineGetter(object, property, Descriptor.get);
}
if (owns(descriptor, "set")) {
defineSetter(object, property, Descriptor.set);
}
}
返回對象;
};
}
// ES5 15.2.3.7
// http ://es5.github.com/#x15.2.3.7
if (!Object.defineProperties) {
Object. DefineProperties = function DefineProperties(object,properties) {
for (properties, DefineProperties(object,properties) {
for (properties, DefineProperties(object,properties) {
for (properties, DefineProperties(object,properties) {
for (properties, DefineProperties(object,properties) {
for (properties, DefineProperties(object,properties) {
for (properties, 時期) {
if (owns(properties, property) && property != "__proto__") {
Object.defineProperty(object,財產,財產[財產]);
}
}
回物件;
};
程式碼如下:
class module extends Base {s1.
}
}
class module extends Base {s1.
}
}
越玩越像java了,不過es6很多瀏覽器不支援
最後推薦的寫法:程式碼如下:
if (!Object.create) {
Object.create = function create(o) {
var F = function(){};
F.prototype = o;
var 結果= new F();
F.prototype = null;
回傳結果;
}
}
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文討論了使用瀏覽器開發人員工具的有效JavaScript調試,專注於設置斷點,使用控制台和分析性能。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

本文說明瞭如何使用源地圖通過將其映射回原始代碼來調試JAVASCRIPT。它討論了啟用源地圖,設置斷點以及使用Chrome DevTools和WebPack之類的工具。

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...
