JavaScript中的類與實(shí)例實(shí)現(xiàn)方法
本文實(shí)例講述了JavaScript中的類與實(shí)例實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
JavaScript 中沒有父類, 子類的概念, 也沒有class 和 instance 的概念, 全靠 prototype chain來實(shí)現(xiàn)繼承. 當(dāng)查找一個(gè)對象的屬性時(shí), JavaScript 會(huì)向上遍歷 prototype chain, 直到找到對應(yīng)的屬性為止. 有幾種方法, 可以使得 JavaScript 模擬出 class 和 instance 的概念.
1. 直接使用構(gòu)造函數(shù)來創(chuàng)建對象, 在構(gòu)造函數(shù)內(nèi)部使用 this指代對象實(shí)例.
this.name = "animal";
}
Animal.prototype.makeSound = function() {
console.log("animal sound");
}
[Function]
var animal1 = new Animal();
animal1.name;
'animal'
animal1.makeSound();
animal sound
再看另外一個(gè)例子:
this.x = x;
this.y = y;
}
Point.prototype = {
method1: function() { console.log("method1"); },
method2: function() { console.log("method2"); },
}
{ method1: [Function], method2: [Function] }
var point1 = new Point(10, 20);
point1.method1();
method1
point1.method2();
method2
以上, 先指定好一個(gè)構(gòu)造函數(shù)對象的 prototype 屬性. 然后 new 一個(gè)該對象實(shí)例, 即可調(diào)用 prototype 中指定的方法.
2. 使用 Object.create()方法來創(chuàng)建對象
name: "animal",
makeSound: function() { console.log("animal sound"); },
}
var animal2 = Object.create(Animal);
animal2.name;
'animal'
console.log(animal2.name);
animal
animal2.makeSound();
animal sound
該方法, 比構(gòu)造函數(shù)的方法更簡便, 但不能實(shí)現(xiàn)私有屬性和私有方法, 且實(shí)例對象之間不能共享數(shù)據(jù), 對 class 的模擬仍不夠全面.
3. 荷蘭程序員 Gabor de Mooij 提出的極簡主義法(minimalist approach). 推薦用法.
init: function() {
var animal = {};
animal.name = "animal";
animal.makeSound = function() { console.log("animal sound"); };
return animal;
}
};
var animal3 = Animal.init();
animal3.name;
'animal'
animal3.makeSound();
animal sound
不使用 prototype 和 this, 僅需要自定義一個(gè)構(gòu)造函數(shù)init. 繼承的實(shí)現(xiàn)也很簡單.
init: function() {
var cat = Animal.init();
cat.name2 = "cat";
cat.makeSound = function() { console.log("cat sound"); };
cat.sleep = function() { console.log("cat sleep"); };
return cat;
}
}
var cat = Cat.init();
cat.name; // 'animal'
cat.name2; // 'cat'
cat.makeSound(); // 類似于方法的重載
cat sound
cat.sleep();
cat sleep
私有屬性和私有方法的使用:
init: function() {
var animal = {};
var sound = "private animal sound"; // 私有屬性
animal.makeSound = function() { console.log(sound); };
return animal;
}
};
var animal4 = Animal.init();
Animal.sound; // undefined 私有屬性只能通過對象自身的方法來讀取.
animal.sound; // undefined 私有屬性只能通過對象自身的方法來讀取
animal4.makeSound();
private animal sound
只要不是定義在animal對象上的屬性和方法都是私有的, 外界不能訪問.
類與實(shí)例之間, 可以做到數(shù)據(jù)共享.
sound: "common animal sound",
init: function() {
var animal = {};
animal.commonSound = function() { console.log(Animal.sound); };
animal.changeSound = function() { Animal.sound = "common animal sound changed"; };
return animal;
}
}
var animal5 = Animal.init();
var animal6 = Animal.init();
Animal.sound; // 可以視為類屬性
'common animal sound'
animal5.sound; // 實(shí)例對象不能訪問類屬性
undefined
animal6.sound;
undefined
animal5.commonSound();
common animal sound
animal6.commonSound();
common animal sound
animal5.changeSound(); // 修改類屬性
undefined
Animal.sound;
'common animal sound'
animal5.commonSound();
common animal sound
animal6.commonSound();
common animal sound
如 Animal.sound 就是類與實(shí)例的共有屬性, 可以視為類屬性和類方法.
若一個(gè)實(shí)例修改了該共有屬性, 則該類和其他實(shí)例的共有屬性也對應(yīng)修改了.
綜上, 就是 JavaScript 中模擬的 class 和 instance 的概念和用法.
希望本文所述對大家的javascript程序設(shè)計(jì)有所幫助。
- Js類的靜態(tài)方法與實(shí)例方法區(qū)分及jQuery拓展的兩種方法
- JavaScript面向?qū)ο笾接徐o態(tài)變量實(shí)例分析
- js靜態(tài)方法與實(shí)例方法分析
- JavaScript創(chuàng)建類/對象的幾種方式概述及實(shí)例
- javascript定義類和類的實(shí)現(xiàn)實(shí)例詳解
- 淺談js函數(shù)中的實(shí)例對象、類對象、局部變量(局部函數(shù))
- JavaScript類繼承及實(shí)例化的方法
- JavaScript 實(shí)現(xiàn)類的多種方法實(shí)例
- JS中類的靜態(tài)方法,靜態(tài)變量,實(shí)例方法,實(shí)例變量區(qū)別與用法實(shí)例分析
相關(guān)文章
IntersectionObserver判斷是否在可視區(qū)域詳解
這篇文章主要為大家介紹了IntersectionObserver判斷是否在可視區(qū)域詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10javascript中的綁定與解綁函數(shù)應(yīng)用示例
本文為大家詳細(xì)介紹下javascript中綁定與解綁函數(shù)在Ie及Mozilla中的應(yīng)用,感興趣的各位可以參考下哈,希望對大家有所幫助2013-06-06原生Javascript封裝的一個(gè)AJAX函數(shù)分享
這篇文章主要介紹了原生Javascript封裝的一個(gè)AJAX函數(shù)分享,本文是實(shí)際項(xiàng)目中提取出來的,簡單易用,需要的朋友可以參考下2014-10-10javascript實(shí)現(xiàn)圖片預(yù)加載和懶加載
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)圖片預(yù)加載和懶加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03JS apply用法總結(jié)和使用場景實(shí)例分析
這篇文章主要介紹了JS apply用法總結(jié)和使用場景,結(jié)合實(shí)例形式分析了JS apply的基本功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03