亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解析Javascript中難以理解的11個(gè)問(wèn)題

 更新時(shí)間:2013年12月09日 09:10:16   作者:  
這篇文章主要是對(duì)Javascript中難以理解的11個(gè)問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助

1.原始值與引用值

原始值存放在棧里, 引用值存放在堆里. 如程序:

復(fù)制代碼 代碼如下:

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

var num = 10;
var bol = true;
var str = "abc";
var obj = new Object();
var arr = ['a','b','c'];
var person = new Person(100,"笨蛋的座右銘",25);

2.undefined和null

undefined: 變量未定義; 是Undefined類(lèi)型的專(zhuān)屬值;

null:引用未分配; 是Null類(lèi)型的專(zhuān)屬值.

typeof(undefined) == undefined;
typeof(null) == object;
undefined==null;
undefined!==null;
null instanceof Object == false;
undefined instanceof Object == false;

雖然有Undefined和Null類(lèi)型, 但是通過(guò)下面的例子說(shuō)明這兩個(gè)類(lèi)型是不可見(jiàn)的, 也就是說(shuō)我們只能使用他們的值:

alert(undefined instanceof Undefined);
alert(null instanceof Null);

3.偽數(shù)組

特點(diǎn):
1) 具有l(wèi)ength屬性;

2) 像數(shù)組一樣按索引順序存取數(shù)據(jù);

3) 不具備數(shù)組特有的操作數(shù)據(jù)的方法如push, pop, slice...

偽數(shù)組都可以通過(guò)Array.prototype.slice轉(zhuǎn)換為真正的數(shù)組:

var faceArray = {0: 'a', 1: 'b', length: 2}//標(biāo)準(zhǔn)的偽數(shù)組;

var realArray = Array.prototype.slice.call(fakeArray);

js中的偽數(shù)組:arguments, node.childNodes, document.getElementsByTagName()...

IE中的問(wèn)題 : IE中node.childNodes是不能用slice轉(zhuǎn)化的.

Jquery中的偽數(shù)組 : Jquery本身就是一個(gè)偽數(shù)組:

alert($('.class1').length); alert($('.class1').[0].tagName);

4.關(guān)于簡(jiǎn)單類(lèi)型的字面量

var a = 1; b = true, c = "ccc";

字面量看起來(lái)有類(lèi)型

alert(typeof a);//number
alert(typeof b);//boolean
alert(typeof c);//string

但是通過(guò)instanceof卻測(cè)不出來(lái)

alert(a instanceof Number)//false
alert(a instanceof Object)//false
alert(b instanceof Boolean)//false
alert(b instanceof Object)//false
alert(c instanceof String)//false
alert(c instanceof Object)//false

5.函數(shù)的prototype屬性和對(duì)象實(shí)例的內(nèi)部prototype屬性

每個(gè)function(構(gòu)造函數(shù))都有一個(gè)prototype屬性, 每個(gè)對(duì)象實(shí)例都有一個(gè)不可見(jiàn)的(mozilla把它公開(kāi)了, 可以通過(guò)__proto__來(lái)取得)內(nèi)部的prototype屬性, 它指向構(gòu)造函數(shù)的prototype屬性. prototype還可以有它自己的prototype屬性, 這構(gòu)成了prototype鏈,  Object是最頂?shù)膶?duì)象, 所以所有的prototype鏈最終會(huì)指向Object.prototype. 當(dāng)訪問(wèn)對(duì)象實(shí)例的屬性/方法的時(shí)候, 從對(duì)象實(shí)例自己開(kāi)始搜索, 若果搜索不到, 沿著prototype鏈向上搜索, 直到Object.prototype.prototype == null 為止.

6.構(gòu)造函數(shù)的一個(gè)小秘密

復(fù)制代碼 代碼如下:

var s = new function(){return "sss"};
alert(s);//[object Object]
s = new function(){return new String("sss")};
alert(s);//sss

關(guān)于這段代碼的解釋:

只要 new 表達(dá)式之后的 constructor 返回(return)一個(gè)引用對(duì)象(數(shù)組,對(duì)象,函數(shù)等),都將覆蓋new創(chuàng)建的匿名對(duì)象,如果返回(return)一個(gè)原始類(lèi)型(無(wú) return 時(shí)其實(shí)為 return 原始類(lèi)型 undefined),那么就返回 new 創(chuàng)建的匿名對(duì)象.


7.對(duì)象的創(chuàng)建的過(guò)程

復(fù)制代碼 代碼如下:

function Person(name){
        this.name = name;   
}
Person.prototype = {
        getName: function(){return this.name}   
};

var p = new Person('zhangsan');


解密p的創(chuàng)建過(guò)程:

◦創(chuàng)建一個(gè)build-in object對(duì)象obj并初始化;

◦將p的內(nèi)部[[Prototype]]指向Person.prototype;

◦將p作為this,使用arguments參數(shù)調(diào)用Person的內(nèi)部[[Call]]方法, 即執(zhí)行Person函數(shù)體, 并返回返回值, 如果沒(méi)有return, 則返回undefined;

◦如果前一步返回的是Object類(lèi)型, 則返回這個(gè)值給p, 否則返回obj.

8.對(duì)象的自有屬性和繼承屬性

復(fù)制代碼 代碼如下:

function Person(name){
        this.name = name;   
}
Person.prototype = {
        type: 'human',
        getName: function(){return this.name}   
};
var p = new Person('zhangsan');
alert(p.hasOwnProperty('type'));//false
p.type = 'ren';
alert(p.hasOwnProperty('type'));//true

運(yùn)行結(jié)果很明確,對(duì)象的屬性無(wú)法修改其原型中的同名屬性,而只會(huì)自身創(chuàng)建一個(gè)同名屬性并為其賦值。

9.函數(shù)對(duì)象的創(chuàng)建過(guò)程

創(chuàng)建一個(gè)build-in object對(duì)象fn;

將fn的內(nèi)部[[Prototype]]設(shè)為Function.prototype;

設(shè)置內(nèi)部的[[Call]]屬性,它是內(nèi)部實(shí)現(xiàn)的一個(gè)方法,處理函數(shù)調(diào)用的邏輯。(簡(jiǎn)單的理解為指向函數(shù)體);

設(shè)置fn.length為funArgs.length,如果函數(shù)沒(méi)有參數(shù),則將fn.length設(shè)置為0;

fn.prototype的constructor指向fn自己;

返回fn.

10.instanceof的原理

查看a是不是B的實(shí)例, 就是看B的prototype(構(gòu)造函數(shù)的prototype屬性)指向的對(duì)象在不在a的原形鏈上.

11.關(guān)于Function和Object的猜測(cè)

alert(Function instanceof Function);//true
alert(Function instanceof Object);//true  
alert(Object instanceof Function);//true
alert(Object instanceof Object);//true

想了好久, 沒(méi)有想透......

相關(guān)文章

最新評(píng)論