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

首頁 > 編程 > JavaScript > 正文

JavaScript精煉之構(gòu)造函數(shù) Constructor及Constructor屬性詳解

2019-11-20 11:20:31
字體:
供稿:網(wǎng)友

除了創(chuàng)建對象,構(gòu)造函數(shù)(constructor) 還做了另一件有用的事情―自動為創(chuàng)建的新對象設(shè)置了原型對象(prototype object) 。原型對象存放于 ConstructorFunction.prototype 屬性中。

例如,我們重寫之前例子,使用構(gòu)造函數(shù)創(chuàng)建對象“b”和“c”,那么對象”a”則扮演了“Foo.prototype”這個角色:

// 構(gòu)造函數(shù)function Foo(y) { // 構(gòu)造函數(shù)將會以特定模式創(chuàng)建對象:被創(chuàng)建的對象都會有"y"屬性 this.y = y;}// "Foo.prototype"存放了新建對象的原型引用// 所以我們可以將之用于定義繼承和共享屬性或方法// 所以,和上例一樣,我們有了如下代碼:// 繼承屬性"x"Foo.prototype.x = ;// 繼承方法"calculate"Foo.prototype.calculate = function (z) { return this.x + this.y + z;};// 使用foo模式創(chuàng)建 "b" and "c"var b = new Foo();var c = new Foo();// 調(diào)用繼承的方法b.calculate(); // c.calculate(); // // 讓我們看看是否使用了預(yù)期的屬性console.log( b.__proto__ === Foo.prototype, // true c.__proto__ === Foo.prototype, // true // "Foo.prototype"自動創(chuàng)建了一個特殊的屬性"constructor" // 指向a的構(gòu)造函數(shù)本身 // 實例"b"和"c"可以通過授權(quán)找到它并用以檢測自己的構(gòu)造函數(shù) b.constructor === Foo, // true c.constructor === Foo, // true Foo.prototype.constructor === Foo // true b.calculate === b.__proto__.calculate, // true b.__proto__.calculate === Foo.prototype.calculate // true);

上述代碼可表示為如下的關(guān)系:

構(gòu)造函數(shù)與對象之間的關(guān)系

上述圖示可以看出,每一個object都有一個prototype. 構(gòu)造函數(shù)Foo也擁有自己的__proto__, 也就是Function.prototype, 而Function.prototype的__proto__指向了Object.prototype. 重申一遍,F(xiàn)oo.prototype只是一個顯式的屬性,也就是b和c的__proto__屬性。

這個問題完整和詳細的解釋有兩個部分:

面向?qū)ο缶幊?一般理論(OOP. The general theory),描述了不同的面向?qū)ο蟮姆妒脚c風格(OOP paradigms and stylistics),以及與ECMAScript的比較。

面向?qū)ο缶幊?ECMAScript實現(xiàn)(OOP. ECMAScript implementation), 專門講述了ECMAScript中的面向?qū)ο缶幊獭?br />現(xiàn)在,我們已經(jīng)了解了基本的object原理,那么我們接下去來看看ECMAScript里面的程序執(zhí)行環(huán)境[runtime program execution]. 這就是通常稱為的“執(zhí)行上下文堆棧”[execution context stack]。每一個元素都可以抽象的理解為object。你也許發(fā)現(xiàn)了,沒錯,在ECMAScript中,幾乎處處都能看到object的身影。

下面給大家介紹JavaScript constructor 屬性詳解

對象的constructor屬性用于返回創(chuàng)建該對象的函數(shù),也就是我們常說的構(gòu)造函數(shù)。

在JavaScript中,每個具有原型的對象都會自動獲得constructor屬性。除了arguments、Enumerator、Error、Global、Math、RegExp、Regular Expression等一些特殊對象之外,其他所有的JavaScript內(nèi)置對象都具備constructor屬性。例如:Array、Boolean、Date、Function、Number、Object、String等。所有主流瀏覽器均支持該屬性。

語法

object.constructor

返回值

對象的constructor屬性返回創(chuàng)建該對象的函數(shù)的引用。

示例&說明

以下代碼中的[native code],表示這是JavaScript的底層內(nèi)部代碼實現(xiàn),無法顯示代碼細節(jié)。

// 字符串:String()var str = "張三";document.writeln(str.constructor); // function String() { [native code] }document.writeln(str.constructor === String); // true// 數(shù)組:Array()var arr = [1, 2, 3];document.writeln(arr.constructor); // function Array() { [native code] }document.writeln(arr.constructor === Array); // true// 數(shù)字:Number()var num = 5;document.writeln(num.constructor); // function Number() { [native code] }document.writeln(num.constructor === Number); // true// 自定義對象:Person()function Person(){  this.name = "CodePlayer";}var p = new Person();document.writeln(p.constructor); // function Person(){ this.name = "CodePlayer"; }document.writeln(p.constructor === Person); // true// JSON對象:Object()var o = { "name" : "張三"};document.writeln(o.constructor); // function Object() { [native code] }document.writeln(o.constructor === Object); // true// 自定義函數(shù):Function()function foo(){  alert("CodePlayer");}document.writeln(foo.constructor); // function Function() { [native code] }document.writeln(foo.constructor === Function); // true// 函數(shù)的原型:bar()function bar(){  alert("CodePlayer");}document.writeln(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }document.writeln(bar.prototype.constructor === bar); // true
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 二连浩特市| 自治县| 汾阳市| 寿阳县| 灵璧县| 中阳县| 昌吉市| 荥经县| 长沙市| 泸州市| 洛宁县| 华阴市| 漳平市| 额尔古纳市| 司法| 永嘉县| 涪陵区| 临泉县| 甘南县| 巍山| 深圳市| 卓资县| 资源县| 建瓯市| 威信县| 新营市| 安溪县| 宜城市| 岫岩| 九台市| 池州市| 绩溪县| 肇州县| 玉屏| 托克托县| 阿克苏市| 内江市| 鞍山市| 马公市| 皮山县| 蕲春县|