黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

OO JavaScript

系統 2503 0

如何創建JavaScript Object呢?有如下幾種方法:

一、Constructor Pattern

?

    function Person(name, age, job) {
	this.name = name;
	this.age = age;
	this.job = job;

	this.sayName = function() { 
		return this.name;
	}
};

var p1 = new Person('Nicolas', 29, 'Software Engineer');
var p2 = new Person('Greg', 27, 'Doctor');

//function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.sayName = function () {return this.name;}; }
println(1, Person.toString());
println(2, Person.length);//3
println(3, Person.constructor);//function Function() { [native code] }
println(4, Person.prototype.constructor == Person);//1 true;
println(5, Person.prototype.name);//undefined;
println(6, Person.prototype.age);//undefined;
println(6, Person.prototype.job);//undefined;
println(7, Person.prototype.sayName);//undefined;

println(8, Person.prototype == p1.__proto__);//2 true;
println(9, p2.__proto__ == p1.__proto__);//2 true;
println(10, p2.sayName);//function(){return this.name);
println(11, p2.sayName());//Greg
println(10, p1.sayName);//function(){return this.name);
println(11, p1.sayName());//Nicolas
println(11, p2.sayName == p1.sayName);//false

function println(i, value) {
	document.write(i + ' &nbsp;&nbsp;' + value + '<hr/>');
}
  

?

?

注意:

1. 當JavaScript解析從第一行~第九行的代碼的時候,發現時一段function聲明代碼的時候,會做如下幾個工作

A1:在heap里面生成一個Function對象代表這段function代碼

A2:給這個Function對象,這個對象會自動獲得一個prototype的屬性。這個屬性指向該對象的prototype object。默認說來,所有的prototype object都會自動獲得一個constructor的屬性,該屬性存放指向Function對象的pointer

A3:設定這個Function的名字(因為這里不是一個匿名function)

A4:在global object對象建立Person的屬性,這個屬性的值為指向Function對象的指針。

?

2. 第11行:var p1 = new Person('Nicolas', 29, 'Software Engineer'),這里function作為new的方式呼叫,javascript是如何處理的呢?

A1:首先在heap去new Object,然后以這個Object作為這段code的execute context,加入到scope chain的頭部。此時this-->這個object,然后進入code

A2:this.name = name,this.age = age,this.job=job,這三段代碼,對剛才生成的那個Object添加屬性,并賦值

A3:this.sayName = function(){...},這段會首先在heap里面生成一個function的對象,然后將該對象的pointer賦給剛才生成對象的sayName屬性上

A4:對這個object.__proto__賦值,指向function Person(name,age,job){...},然后將這個object的pointer賦值給stack里面的p1;(注意這個__proto__屬性,可以在firefox,safari,chrome等瀏覽器可以訪問,但在ie等瀏覽器則不能訪問,這是因為實現EMCScript 262的規范不同造成的)

?

?

3.第30行的輸出結果是false,為什么呢?

從上面的分析,我們可以知道在new Person的時候,執行this.sayName=function(){...},會首先去heap里面new Function Object,然后賦值給sayName,所以在p1和p2都會執行這個過程。這樣就會new 2個邏輯上相等的Function Object,但是由于他們的內存位置不同,所以在p1.sayName == p2.sayName的結果為false。這也是Contructor Pattern方式最大的問題,需要產生的Function Object太多了。

?

其內存結構為:

?


OO JavaScript
?


?
?
二,Prototype Pattern

請參看代碼:

    	function Person() {
	};

	//Person是一個Function的對象
	Person.prototype.name = 'Nicolas';
	Person.prototype.age = 29
	Person.prototype.sayName = function() {
		return this.name;
	};

	var p1 = new Person();

	var p2 = new Person();

	println(1, Person.toString());
	println(2, Person.length);
	println(3, Person.constructor);
	println(4, Person.prototype.constructor == Person);//1 true;
	println(5, Person.prototype.name);//Nicolas;
	println(6, Person.prototype.name == p2.name);//Nicolas;
	println(7, Person.prototype.sayName == p2.sayName);//Nicolas;

	println(8, Person.prototype == p1.__proto__);//2 true;
	println(9, p2.__proto__ == p1.__proto__);//2 true;
	println(10, p2.__proto__.sayName == p1.__proto__.sayName);
	println(11, p2.sayName() == 'Nicolas');
	println(12, p1.sayName == p1.sayName);
	println(13, p2.sayName() == p1.sayName());
	
	function println(i, value) {
		document.write(i + ' &nbsp;&nbsp;' + value + '<hr/>');
	}
  

?

?注意:

這里是將property和method都加到Person.prototype所指向的prototype的對象上,因為p1和p2都指向這個prototype對象的指針,所以訪問的時候,他們共享同一片內存區域。對于方法而言是共享了,但是property卻混淆在了一塊兒。參看如下的內存圖:
?
OO JavaScript
?

?

?

?

?

OO JavaScript


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論