JavaScript中幾個(gè)重要的屬性(this、constructor、prototype)介紹
更新時(shí)間:2013年05月19日 15:32:05 作者:
this表示當(dāng)前對(duì)象,如果在全局作用范圍內(nèi)使用this,則指代當(dāng)前頁面對(duì)象window,prototype本質(zhì)上還是一個(gè)JavaScript對(duì)象,constructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)
this
this表示當(dāng)前對(duì)象,如果在全局作用范圍內(nèi)使用this,則指代當(dāng)前頁面對(duì)象window; 如果在函數(shù)中使用this,則this指代什么是根據(jù)運(yùn)行時(shí)此函數(shù)在什么對(duì)象上被調(diào)用。 我們還可以使用apply和call兩個(gè)全局方法來改變函數(shù)中this的具體指向。
先看一個(gè)在全局作用范圍內(nèi)使用this的例子:
<script type=>
console.log( === window);
console.log(window.alert === .alert);
console.log(.parseInt(, 10));
</script>
函數(shù)中的this是在運(yùn)行時(shí)決定的,而不是函數(shù)定義時(shí),如下:
foo() {
console.log(.fruit);
}
fruit = ;
foo();
pack = {
fruit: ,
foo: foo
};
pack.foo();
全局函數(shù)apply和call可以用來改變函數(shù)中this的指向,如下:
foo() {
console.log(.fruit);
}
fruit = ;
pack = {
fruit:
};
foo.apply(window);
foo.apply(pack);
注:apply和call兩個(gè)函數(shù)的作用相同,唯一的區(qū)別是兩個(gè)函數(shù)的參數(shù)定義不同。
因?yàn)樵贘avaScript中函數(shù)也是對(duì)象,所以我們可以看到如下有趣的例子:
foo() {
( === window) {
console.log();
}
}
foo.boo = () {
( === foo) {
console.log();
} ( === window) {
console.log();
}
};
foo();
foo.boo();
foo.boo.apply(window);
prototype
prototype本質(zhì)上還是一個(gè)JavaScript對(duì)象。 并且每個(gè)函數(shù)都有一個(gè)默認(rèn)的prototype屬性。
如果這個(gè)函數(shù)被用在創(chuàng)建自定義對(duì)象的場景中,我們稱這個(gè)函數(shù)為構(gòu)造函數(shù)。 比如下面一個(gè)簡單的場景:
Person(name) {
.name = name;
}
Person.prototype = {
getName: () {
.name;
}
}
zhang = Person();
console.log(zhang.getName());
作為類比,我們考慮下JavaScript中的數(shù)據(jù)類型 - 字符串(String)、數(shù)字(Number)、數(shù)組(Array)、對(duì)象(Object)、日期(Date)等。 我們有理由相信,在JavaScript內(nèi)部這些類型都是作為構(gòu)造函數(shù)來實(shí)現(xiàn)的,比如:
Array() {
}
arr1 = Array(1, 56, 34, 12);
arr2 = [1, 56, 34, 12];
同時(shí)對(duì)數(shù)組操作的很多方法(比如concat、join、push)應(yīng)該也是在prototype屬性中定義的。
實(shí)際上,JavaScript所有的固有數(shù)據(jù)類型都具有只讀的prototype屬性(這是可以理解的:因?yàn)槿绻薷牧诉@些類型的prototype屬性,則哪些預(yù)定義的方法就消失了), 但是我們可以向其中添加自己的擴(kuò)展方法。
Array.prototype.min = () {
min = [0];
( i = 1; i < .length; i++) {
([i] < min) {
min = [i];
}
}
min;
};
console.log([1, 56, 34, 12].min());
注意:這里有一個(gè)陷阱,向Array的原型中添加擴(kuò)展方法后,當(dāng)使用for-in循環(huán)數(shù)組時(shí),這個(gè)擴(kuò)展方法也會(huì)被循環(huán)出來。
下面的代碼說明這一點(diǎn)(假設(shè)已經(jīng)向Array的原型中擴(kuò)展了min方法):
arr = [1, 56, 34, 12];
total = 0;
( i arr) {
total += parseInt(arr[i], 10);
}
console.log(total);
解決方法也很簡單:
arr = [1, 56, 34, 12];
total = 0;
( i arr) {
(arr.hasOwnProperty(i)) {
total += parseInt(arr[i], 10);
}
}
console.log(total);
constructor
constructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。比如下面例子:
arr = [1, 56, 34, 12];
console.log(arr.constructor === Array);
Foo = () { };
console.log(Foo.constructor === Function);
obj = Foo();
console.log(obj.constructor === Foo);
console.log(obj.constructor.constructor === Function);
但是當(dāng)constructor遇到prototype時(shí),有趣的事情就發(fā)生了。
我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(shù)。如下例所示:
Person(name) {
.name = name;
};
Person.prototype.getName = () {
.name;
};
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋), constructor的行為就有點(diǎn)奇怪了,如下示例:
Person(name) {
.name = name;
};
Person.prototype = {
getName: () {
.name;
}
};
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
為什么呢?
原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:
Person.prototype = Object({
getName: () {
.name;
}
});
而constructor始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(shí)Person.prototype.constructor === Object,即是:
Person(name) {
.name = name;
};
Person.prototype = {
getName: () {
.name;
}
};
p = Person();
console.log(p.constructor === Object);
console.log(Person.prototype.constructor === Object);
console.log(p.constructor.prototype.constructor === Object);
怎么修正這種問題呢?方法也很簡單,重新覆蓋Person.prototype.constructor即可:
Person(name) {
.name = name;
};
Person.prototype = Object({
getName: () {
.name;
}
});
Person.prototype.constructor = Person;
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
this表示當(dāng)前對(duì)象,如果在全局作用范圍內(nèi)使用this,則指代當(dāng)前頁面對(duì)象window; 如果在函數(shù)中使用this,則this指代什么是根據(jù)運(yùn)行時(shí)此函數(shù)在什么對(duì)象上被調(diào)用。 我們還可以使用apply和call兩個(gè)全局方法來改變函數(shù)中this的具體指向。
先看一個(gè)在全局作用范圍內(nèi)使用this的例子:
復(fù)制代碼 代碼如下:
<script type=>
console.log( === window);
console.log(window.alert === .alert);
console.log(.parseInt(, 10));
</script>
函數(shù)中的this是在運(yùn)行時(shí)決定的,而不是函數(shù)定義時(shí),如下:
復(fù)制代碼 代碼如下:
foo() {
console.log(.fruit);
}
fruit = ;
foo();
pack = {
fruit: ,
foo: foo
};
pack.foo();
全局函數(shù)apply和call可以用來改變函數(shù)中this的指向,如下:
復(fù)制代碼 代碼如下:
foo() {
console.log(.fruit);
}
fruit = ;
pack = {
fruit:
};
foo.apply(window);
foo.apply(pack);
注:apply和call兩個(gè)函數(shù)的作用相同,唯一的區(qū)別是兩個(gè)函數(shù)的參數(shù)定義不同。
因?yàn)樵贘avaScript中函數(shù)也是對(duì)象,所以我們可以看到如下有趣的例子:
復(fù)制代碼 代碼如下:
foo() {
( === window) {
console.log();
}
}
foo.boo = () {
( === foo) {
console.log();
} ( === window) {
console.log();
}
};
foo();
foo.boo();
foo.boo.apply(window);
prototype
prototype本質(zhì)上還是一個(gè)JavaScript對(duì)象。 并且每個(gè)函數(shù)都有一個(gè)默認(rèn)的prototype屬性。
如果這個(gè)函數(shù)被用在創(chuàng)建自定義對(duì)象的場景中,我們稱這個(gè)函數(shù)為構(gòu)造函數(shù)。 比如下面一個(gè)簡單的場景:
復(fù)制代碼 代碼如下:
Person(name) {
.name = name;
}
Person.prototype = {
getName: () {
.name;
}
}
zhang = Person();
console.log(zhang.getName());
作為類比,我們考慮下JavaScript中的數(shù)據(jù)類型 - 字符串(String)、數(shù)字(Number)、數(shù)組(Array)、對(duì)象(Object)、日期(Date)等。 我們有理由相信,在JavaScript內(nèi)部這些類型都是作為構(gòu)造函數(shù)來實(shí)現(xiàn)的,比如:
Array() {
}
arr1 = Array(1, 56, 34, 12);
arr2 = [1, 56, 34, 12];
同時(shí)對(duì)數(shù)組操作的很多方法(比如concat、join、push)應(yīng)該也是在prototype屬性中定義的。
實(shí)際上,JavaScript所有的固有數(shù)據(jù)類型都具有只讀的prototype屬性(這是可以理解的:因?yàn)槿绻薷牧诉@些類型的prototype屬性,則哪些預(yù)定義的方法就消失了), 但是我們可以向其中添加自己的擴(kuò)展方法。
Array.prototype.min = () {
min = [0];
( i = 1; i < .length; i++) {
([i] < min) {
min = [i];
}
}
min;
};
console.log([1, 56, 34, 12].min());
注意:這里有一個(gè)陷阱,向Array的原型中添加擴(kuò)展方法后,當(dāng)使用for-in循環(huán)數(shù)組時(shí),這個(gè)擴(kuò)展方法也會(huì)被循環(huán)出來。
下面的代碼說明這一點(diǎn)(假設(shè)已經(jīng)向Array的原型中擴(kuò)展了min方法):
arr = [1, 56, 34, 12];
total = 0;
( i arr) {
total += parseInt(arr[i], 10);
}
console.log(total);
解決方法也很簡單:
arr = [1, 56, 34, 12];
total = 0;
( i arr) {
(arr.hasOwnProperty(i)) {
total += parseInt(arr[i], 10);
}
}
console.log(total);
constructor
constructor始終指向創(chuàng)建當(dāng)前對(duì)象的構(gòu)造函數(shù)。比如下面例子:
復(fù)制代碼 代碼如下:
arr = [1, 56, 34, 12];
console.log(arr.constructor === Array);
Foo = () { };
console.log(Foo.constructor === Function);
obj = Foo();
console.log(obj.constructor === Foo);
console.log(obj.constructor.constructor === Function);
但是當(dāng)constructor遇到prototype時(shí),有趣的事情就發(fā)生了。
我們知道每個(gè)函數(shù)都有一個(gè)默認(rèn)的屬性prototype,而這個(gè)prototype的constructor默認(rèn)指向這個(gè)函數(shù)。如下例所示:
復(fù)制代碼 代碼如下:
Person(name) {
.name = name;
};
Person.prototype.getName = () {
.name;
};
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
當(dāng)時(shí)當(dāng)我們重新定義函數(shù)的prototype時(shí)(注意:和上例的區(qū)別,這里不是修改而是覆蓋), constructor的行為就有點(diǎn)奇怪了,如下示例:
復(fù)制代碼 代碼如下:
Person(name) {
.name = name;
};
Person.prototype = {
getName: () {
.name;
}
};
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
為什么呢?
原來是因?yàn)楦采wPerson.prototype時(shí),等價(jià)于進(jìn)行如下代碼操作:
復(fù)制代碼 代碼如下:
Person.prototype = Object({
getName: () {
.name;
}
});
而constructor始終指向創(chuàng)建自身的構(gòu)造函數(shù),所以此時(shí)Person.prototype.constructor === Object,即是:
復(fù)制代碼 代碼如下:
Person(name) {
.name = name;
};
Person.prototype = {
getName: () {
.name;
}
};
p = Person();
console.log(p.constructor === Object);
console.log(Person.prototype.constructor === Object);
console.log(p.constructor.prototype.constructor === Object);
怎么修正這種問題呢?方法也很簡單,重新覆蓋Person.prototype.constructor即可:
復(fù)制代碼 代碼如下:
Person(name) {
.name = name;
};
Person.prototype = Object({
getName: () {
.name;
}
});
Person.prototype.constructor = Person;
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);
相關(guān)文章
javaScript arguments 對(duì)象使用介紹
函數(shù)體內(nèi)可以通過 arguments 對(duì)象來接收傳遞進(jìn)來的參數(shù),下面有個(gè)不錯(cuò)的示例,大家可以感受下2013-10-10關(guān)于鍵盤事件中keyCode、which和charCode 的兼容性測試
關(guān)于鍵盤事件中keyCode、which和charCode 的兼容性測試...2006-12-12一起來學(xué)習(xí)一下JavaScript的事件流
這篇文章主要為大家詳細(xì)介紹了JavaScript的事件流,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01