第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何理解 JS 函數(shù)在定義的時(shí)候不執(zhí)行?

如何理解 JS 函數(shù)在定義的時(shí)候不執(zhí)行?

富國(guó)滬深 2019-04-21 20:39:02
函數(shù)1:一個(gè)函數(shù)聲明:functionff(a){alert(a);}函數(shù)2:一個(gè)函數(shù)表達(dá)式:varf=function(){return(1+2);}函數(shù)只在調(diào)用時(shí)候執(zhí)行花括號(hào)內(nèi)的語(yǔ)句,那么在定義函數(shù)的時(shí)候,都做了哪些工作呢?比如函數(shù)2,varf=function(){return(1+2);}是原封不動(dòng)的把{}花括號(hào)內(nèi)的代碼賦值給一個(gè)變量嗎?還是說,會(huì)計(jì)算一下1+2==》varf=function(){return(3);},待調(diào)用時(shí)返回3。我在網(wǎng)上和書上都找了相關(guān)知識(shí),但好像這個(gè)問題太簡(jiǎn)單了,可以說是一帶而過,求解答,謝謝。
查看完整描述

2 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

在定義函數(shù)的時(shí)候,都做了哪些工作呢?
Thereisnosillyquestion!這是一個(gè)好問題啊,ECMA標(biāo)準(zhǔn)里面有一節(jié)專門講的就是這個(gè)問題。
我在網(wǎng)上和書上都找了相關(guān)知識(shí)
你確定你找對(duì)地方了么?這種問題不應(yīng)該去看ECMA標(biāo)準(zhǔn)么?
簡(jiǎn)單來說,在JavaScript中,因?yàn)楹瘮?shù)是一等對(duì)象,定義函數(shù)的時(shí)候,實(shí)際上就是創(chuàng)建了一個(gè)對(duì)象,就這么簡(jiǎn)單!不過為了保證這個(gè)對(duì)象能夠調(diào)用,它有幾個(gè)特殊的內(nèi)部屬性/方法:
[Call]
[[Construct]]
[[Scope]]
[[Code]]
[[FormalParameters]]
當(dāng)調(diào)用函數(shù)的時(shí)候,實(shí)際上就是調(diào)用該函數(shù)對(duì)象的[[Call]]內(nèi)部方法;
當(dāng)使用new調(diào)用函數(shù)的時(shí)候,實(shí)際上就是調(diào)用該函數(shù)對(duì)象的[[Construct]]內(nèi)部方法。
定義函數(shù)時(shí),{}之間的東西,不管是什么,哪怕是空的,全部原封不動(dòng)地保存在[[Code]]內(nèi)部屬性里;
13.2CreatingFunctionObjects
GivenanoptionalparameterlistspecifiedbyFormalParameterList,abodyspecifiedbyFunctionBody,aLexicalEnvironmentspecifiedbyScope,andaBooleanflagStrict,aFunctionobjectisconstructedasfollows:
CreateanewnativeECMAScriptobjectandletFbethatobject.
Setalltheinternalmethods,exceptfor[[Get]],ofFasdescribedin8.12.
Setthe[[Class]]internalpropertyofFto"Function".
Setthe[[Prototype]]internalpropertyofFtothestandardbuilt-inFunctionprototypeobjectasspecifiedin15.3.3.1.
Setthe[[Get]]internalpropertyofFasdescribedin15.3.5.4.
Setthe[[Call]]internalpropertyofFasdescribedin13.2.1.
Setthe[[Construct]]internalpropertyofFasdescribedin13.2.2.
Setthe[[HasInstance]]internalpropertyofFasdescribedin15.3.5.3.
Setthe[[Scope]]internalpropertyofFtothevalueofScope.
LetnamesbeaListcontaining,inlefttorighttextualorder,theStringscorrespondingtotheidentifiersofFormalParameterList.Ifnoparametersarespecified,letnamesbetheemptylist.
Setthe[[FormalParameters]]internalpropertyofFtonames.Setthe[[Code]]internalpropertyofFtoFunctionBody.
Setthe[[Extensible]]internalpropertyofFtotrue.
LetlenbethenumberofformalparametersspecifiedinFormalParameterList.Ifnoparametersarespecified,letlenbe0.
Callthe[[DefineOwnProperty]]internalmethodofFwitharguments"length",PropertyDescriptor{[[Value]]:len,[[Writable]]:false,[[Enumerable]]:false,[[Configurable]]:false},andfalse.
LetprotobetheresultofcreatinganewobjectaswouldbeconstructedbytheexpressionnewObject()whereObjectisthestandardbuilt-inconstructorwiththatname.
Callthe[[DefineOwnProperty]]internalmethodofprotowitharguments"constructor",PropertyDescriptor{[[Value]]:F,{[[Writable]]:true,[[Enumerable]]:false,[[Configurable]]:true},andfalse.
Callthe[[DefineOwnProperty]]internalmethodofFwitharguments"prototype",PropertyDescriptor{[[Value]]:proto,{[[Writable]]:true,[[Enumerable]]:false,[[Configurable]]:false},andfalse.
IfStrictistrue,then
Letthrowerbethe[[ThrowTypeError]]functionObject(13.2.3).
Callthe[[DefineOwnProperty]]internalmethodofFwitharguments"caller",PropertyDescriptor{[[Get]]:thrower,[[Set]]:thrower,[[Enumerable]]:false,[[Configurable]]:false},andfalse.
Callthe[[DefineOwnProperty]]internalmethodofFwitharguments"arguments",PropertyDescriptor{[[Get]]:thrower,[[Set]]:thrower,[[Enumerable]]:false,[[Configurable]]:false},andfalse.
ReturnF.
NOTEAprototypepropertyisautomaticallycreatedforeveryfunction,toallowforthepossibilitythatthefunctionwillbeusedasaconstructor.
調(diào)用函數(shù)時(shí),LetresultbetheresultofevaluatingtheFunctionBodythatisthevalueofF's[[Code]]internalproperty.,才會(huì)對(duì)[[Code]]屬性求值(evaluate)。
13.2.1[[Call]]
Whenthe[[Call]]internalmethodforaFunctionobjectFiscalledwithathisvalueandalistofarguments,thefollowingstepsaretaken:
LetfuncCtxbetheresultofestablishinganewexecutioncontextforfunctioncodeusingthevalueofF's[[FormalParameters]]internalproperty,thepassedargumentsListargs,andthethisvalueasdescribedin10.4.3.LetresultbetheresultofevaluatingtheFunctionBodythatisthevalueofF's[[Code]]internalproperty.IfFdoesnothavea[[Code]]internalpropertyorifitsvalueisanemptyFunctionBody,thenresultis(normal,undefined,empty).
ExittheexecutioncontextfuncCtx,restoringthepreviousexecutioncontext.
Ifresult.typeisthrowthenthrowresult.value.
Ifresult.typeisreturnthenreturnresult.value.
Otherwiseresult.typemustbenormal.Returnundefined.
                            
查看完整回答
反對(duì) 回復(fù) 2019-04-21
  • 2 回答
  • 0 關(guān)注
  • 610 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)