欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript函數(shù)及其prototype詳解

 更新時(shí)間:2023年03月14日 08:39:41   作者:此時(shí)奮飛  
這篇文章主要介紹了JavaScript函數(shù)及其prototype詳解的相關(guān)資料,需要的朋友可以參考下

一、定義

函數(shù)可以通過(guò)聲明定義,也可以是一個(gè)表達(dá)式。

(1)函數(shù)聲明式:

分號(hào)是用來(lái)分隔可執(zhí)行JavaScript語(yǔ)句,由于函數(shù)聲明不是一個(gè)可執(zhí)行語(yǔ)句,所以不以分號(hào)結(jié)束。

function a(){
console.log('bbb');
}

(2)函數(shù)表達(dá)式:

函數(shù)表達(dá)式以分號(hào)結(jié)尾,因?yàn)樗且粋€(gè)執(zhí)行語(yǔ)句。

var a = function(){
console.log('aaa');
}

(3)聲明式變體:

var a = function a(){
console.log('bbb');
}

完全等價(jià)于 function b(){}

二、函數(shù)執(zhí)行

JavaScript解析器將當(dāng)前作用域內(nèi)聲明的所有變量和函數(shù)都會(huì)放到作用域的開(kāi)始處,但是,只有變量的聲明被提前到作用域的開(kāi)始處了,而賦值操作被保留在原處。

示例1:

var a = function(){
console.log('aaa');
}
function a(){
console.log('bbb');
}
a(); //結(jié)果:aaa

示例2:

(function() {
console.log(noSuchVariable);
})();
//報(bào)錯(cuò),因?yàn)閚oSuchVariable變量根本沒(méi)定義
</pre><pre name="code" class="javascript">(function() {
console.log(declaredLater); //"undefined"
var declaredLater = "Now it's defined!";
console.log(declaredLater); //"Now it's defined!"
})();
//上面這段代碼是正確的,沒(méi)有任何問(wèn)題。

示例3:

var a; // 聲明一個(gè)變量,標(biāo)識(shí)符為a
function a() {} // 聲明一個(gè)函數(shù),標(biāo)示符也為a
alert(typeof a); // function

function a() {}
var a;
alert(typeof a); //function

function a() {}
var a = 1;
alert(typeof a); //number

var a = 1;
function a() {}
alert(typeof a); //number

總結(jié):

  • 變量的聲明被提前到作用域頂部,賦值保留在原地;
  • 聲明式函數(shù)整個(gè)“被提前”;
  • 函數(shù)表達(dá)式只有變量“被提前”了,函數(shù)沒(méi)有“被提前”;
  • 函數(shù)會(huì)首先被提升,然后才是變量,重復(fù)的var(變量)聲明會(huì)被忽略掉;
  • 同名情況下,變量先“被提前”,函數(shù)再“再被提前”(即函數(shù)覆蓋變量);
  • 賦值會(huì)根據(jù)前后順序進(jìn)行覆蓋!

三、函數(shù)是對(duì)象[typeof instanceof]

在 JavaScript 中使用 typeof 操作符判斷函數(shù)類型將返回"function"。但JavaScript函數(shù)描述為一個(gè)對(duì)象更加準(zhǔn)確。JavaScript函數(shù)有屬性和方法。

(1)typeof區(qū)分對(duì)象和其他原始值

JavaScript的數(shù)據(jù)類型分為:原始類型(數(shù)字、字符串、布爾值、null、undefined)和對(duì)象類型。

typeof[任何類型]返回值(boolean、string、number、object、undefined)和 function

typeof 2; //'number'
typeof '2'; //'string'
typeof true; //'boolean'
typeof []; //'object'
typeof null; //'object'
typeof undefined; //'undefined'
var a = function(){}; typeof a; //'function'

對(duì)于所有內(nèi)置可執(zhí)行對(duì)象,typeof一律返回“function”

typeof new Number(2); //'object'
typeof new String('2'); //'object'
typeof new Boolean(true); //'object'

(2)instanceof[對(duì)象 instanceof 類]判斷一個(gè)對(duì)象是否為類的實(shí)例

[] instanceof Object; //true
2 instanceof Number; //false
new Number(2) instanceof Number; //true

示例:

var a = function(){};
var b = function(){};
a.prototype = new b();
new a() instanceof b; //true
new a() instanceof Object; //true

首先確定b.prototype,如果b.prototype在a對(duì)象的原型鏈中,返回true,否則返回false。

注意;所有對(duì)象都是Object的實(shí)例。

(3)擴(kuò)展 Object.prototype.toString.call([]) === '[object Array]';

四、prototype

JavaScript 對(duì)象有兩種不同的屬性,一種是對(duì)象自身的屬性,另外一種是繼承于原型鏈上的屬性。

JavaScript中,類的所有實(shí)例對(duì)象都從同一個(gè)原型對(duì)象上繼承屬性,因此,原型對(duì)象是類的核心。

(1)只有函數(shù)有prototype,對(duì)象沒(méi)有

var b = {};
b.prototype.bbb='bbb'; //報(bào)錯(cuò)b.prototype為undefined

function b(){}
b.prototype.bbb='bbb';
console.log(b); //結(jié)果:function b(){}
console.log(b.prototype); //結(jié)果:b {bbb: "bbb"}
console.log(b.bbb); //結(jié)果:undefined
console.log(new b().bbb); //結(jié)果:bbb

(2)prototype是函數(shù)的內(nèi)置屬性(用于設(shè)置函數(shù)的原型),__proto__是對(duì)象的內(nèi)置屬性(用于設(shè)置對(duì)象的原型)。

PS:注意區(qū)分函數(shù)和對(duì)象

function test(){}
test.prototype; //結(jié)果:test {}
new test().prototype; //結(jié)果:undefined,對(duì)象沒(méi)有prototype
new test().__proto__; //結(jié)果:test {}

function a(){
this.v='vvv';
}
a.prototype; //a {}
new a.__proto__; //a {}
new a().__proto__ === a.prototype; //true 實(shí)例對(duì)象的__proto__,指向函數(shù)的原型

(3)每個(gè)JavasScript函數(shù)都自動(dòng)擁有一個(gè)prototype屬性。這個(gè)屬性的值是一個(gè)對(duì)象,這個(gè)對(duì)象包含唯一一個(gè)不可枚舉屬性constructor。constructor屬性的值是一個(gè)函數(shù)對(duì)象。

function a(){
this.v='vvv';
}
a.prototype.constructor === a; //true[物理地址一樣]
a.prototype.constructor; //constructor屬性指代這個(gè)類
a.prototype.constructor.__proto__ === a.__proto__ === Function.prototype; //true
Object.prototype.constructor.__proto__ === Object.__proto__ === Function.prototype; //true

PS:所有構(gòu)造器、函數(shù)的__proto__都指向Function.prototype,它是一個(gè)空函數(shù)(Empty function)

(4)實(shí)例

示例1:

function a(){}
function b(){}
a.prototype = {aaa:'aaa'};
b.prototype = a.prototype;
var aa1 = new a();
var bb1 = new b();
//aa1.prototype.aaa='xxx'; //錯(cuò)誤,對(duì)象沒(méi)有原型
a.prototype.aaa = 'xxx';
console.log( aa1.aaa ); //xxx
console.log( bb1.aaa ); //xxx

補(bǔ)充:a===b; //false    a.prototype === b.prototype; //true

實(shí)例2:

function a(){}
function b(){ this.bbb='bbb'}
a.prototype = new b(); //相當(dāng)于a.prototype = {bbb: "bbb"};
console.log(new a().bbb ); //結(jié)果:bbb

補(bǔ)充:a.prototype === b.prototype;  //false

五、實(shí)例方法&靜態(tài)方法

(1)實(shí)例方法

function a(){}
a.prototype.count = 0;
var a1 = new a();
a1.count++;
var a2 = new a();
a2.count++;
a.prototype.count; //0

PS:方法類似 a.prototype.method = function(){}

(2)靜態(tài)方法

function a(){}
a.count = 0;
new a().count(); //undefined,只能使用a.count調(diào)用

PS:方法類似 a.method = function(){}

(3)當(dāng)代碼var p = new Person()執(zhí)行時(shí),new 做了如下幾件事情:

  A. 創(chuàng)建一個(gè)空白對(duì)象

  B. 創(chuàng)建一個(gè)指向Person.prototype的指針

  C. 將這個(gè)對(duì)象通過(guò)this關(guān)鍵字傳遞到構(gòu)造函數(shù)中并執(zhí)行構(gòu)造函數(shù)

六、閉包

函數(shù)定義時(shí)的作用域鏈到函數(shù)執(zhí)行時(shí)依然有效。

示例-典型閉包:

var scope = "global scope";
function checkScope(){
var scope = "local scope";
function f(){
return scope;
}
return f();
}
checkScope(); //local scope

示例:更為常用的閉包

function counter(){
var n=0;
return{
count:function(){
return ++n;
},
reset:function(){
n=0;
}
};
}
var c1 = new counter();
c1.count(); //1
var c2 = new counter();
c2.count(); //1
c1.reset(); //c1置為0,c2不受影響
c2.count(); //2

PS:實(shí)例對(duì)象互相不影響,單個(gè)實(shí)例對(duì)象內(nèi)的方法,共享私有變量。

七、通過(guò)調(diào)用方法的形式來(lái)間接調(diào)用函數(shù):apply&call

function a(){
console.log(this.x+this.y);
}
a.call({x:100,y:100}); //200

function C(){
this.m = function(){
console.log(this.x+this.y);
}
}
var c = new C();
c.m.call({x:100,y:100}); //200

call&apply第一個(gè)實(shí)參是要調(diào)用函數(shù)的母對(duì)象,會(huì)變?yōu)閠his的值。

函數(shù).call(對(duì)象,參數(shù)1,參數(shù)2);

函數(shù).apply(對(duì)象,[參數(shù)1,參數(shù)2]); //數(shù)組形式

補(bǔ)充:

function a(a,b,c,d){

? //將偽數(shù)組轉(zhuǎn)變?yōu)檎鏀?shù)組

? var args = [].slice.call(arguments);

}

八、函數(shù)中this

function a(){
console.log(this); //a {}
function b(){
console.log(this); //window
}
b();
}
new a();

特別注意:函數(shù)內(nèi)部函數(shù)this指向window

此問(wèn)題在嚴(yán)格模式已被修改;在非嚴(yán)格模式中,可使用變量接收外部函數(shù)this供內(nèi)部函數(shù)使用,如var that = this;

九、回調(diào)(函數(shù)當(dāng)參數(shù))

function a(callback){

callback();

}

a(function(){

console.log('aaa');

});

更加靈活,主頁(yè)面只需為子頁(yè)面提供對(duì)應(yīng)接口,具體處理邏輯全部由子頁(yè)面處理。

到此這篇關(guān)于JavaScript函數(shù)及其prototype詳解的文章就介紹到這了,更多相關(guān)JavaScript函數(shù)及其prototype內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JS 加入收藏夾的代碼(主流瀏覽器通用)

    JS 加入收藏夾的代碼(主流瀏覽器通用)

    所有瀏覽器通用的js添加收藏夾代碼,本文整理了多種方法實(shí)現(xiàn)加入收藏夾功能,下面為大家詳細(xì)介紹下實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈
    2013-05-05
  • js時(shí)間日期格式化封裝函數(shù)

    js時(shí)間日期格式化封裝函數(shù)

    這里給大家推薦一款自己封裝的js時(shí)間日期格式化的函數(shù),非常的簡(jiǎn)潔實(shí)用,分享給有需要的小伙伴
    2014-12-12
  • 深入理解JavaScript系列(13) This? Yes,this!

    深入理解JavaScript系列(13) This? Yes,this!

    在這篇文章里,我們將討論跟執(zhí)行上下文直接相關(guān)的更多細(xì)節(jié)。討論的主題就是this關(guān)鍵字。實(shí)踐證明,這個(gè)主題很難,在不同執(zhí)行上下文中this的確定經(jīng)常會(huì)發(fā)生問(wèn)題
    2012-01-01
  • js仿微信公眾平臺(tái)打標(biāo)簽功能

    js仿微信公眾平臺(tái)打標(biāo)簽功能

    這篇文章主要為大家詳細(xì)介紹了js仿微信公眾平臺(tái)打標(biāo)簽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 慎用 somefunction.prototype 分析

    慎用 somefunction.prototype 分析

    在寫(xiě) JavaScript 腳本的時(shí)候,為了創(chuàng)建一個(gè)類,如果不使用框架,一般情況我們都會(huì)使用 prototype 來(lái)給要?jiǎng)?chuàng)建的類增加公有方法
    2009-06-06
  • javascript拖拽應(yīng)用實(shí)例(二)

    javascript拖拽應(yīng)用實(shí)例(二)

    這篇文章主要為大家詳細(xì)介紹了javascript拖拽應(yīng)用實(shí)例,拖拽條拖到底,驗(yàn)證碼顯示出來(lái),感興趣的小伙伴們可以參考一下
    2016-03-03
  • Bootstrap-table自定義可編輯每頁(yè)顯示記錄數(shù)

    Bootstrap-table自定義可編輯每頁(yè)顯示記錄數(shù)

    這篇文章主要介紹了Bootstrap-table自定義可編輯每頁(yè)顯示記錄數(shù)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • 無(wú)刷新預(yù)覽所選擇的圖片示例代碼

    無(wú)刷新預(yù)覽所選擇的圖片示例代碼

    這篇文章主要介紹了無(wú)刷新預(yù)覽所選擇的圖片的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-04-04
  • JavaScript替換當(dāng)前頁(yè)面的方法

    JavaScript替換當(dāng)前頁(yè)面的方法

    這篇文章主要介紹了JavaScript替換當(dāng)前頁(yè)面的方法,涉及javascript中replace方法的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • 關(guān)于JavaScript中forEach和each用法淺析

    關(guān)于JavaScript中forEach和each用法淺析

    這篇文章主要給大家介紹了關(guān)于JavaScript中forEach和each使用方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07

最新評(píng)論