補(bǔ)充:
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
這個題還有一個函數(shù)同名覆蓋的知識點(diǎn)
var bb=1;
function aa(bb) {
bb=2;
alert(bb);//注意這里是重新聲明了一個新的bb,所以里面雖然把bb改為2了,但是絲毫不影響外面的bb蚕断,他們不是一個bb
}
aa(bb);
alert(bb)
Paste_Image.png
注意函數(shù)里面打印a時(shí)是找的參數(shù)a,不是局部變量a
Paste_Image.png
這個后面的f把前面的f(聲明前置了)覆蓋了所以無論怎么執(zhí)行匿垄,前面哪一個都不會再執(zhí)行了
var foo={n:1}
(function () {
console.log(foo.n)
foo.n=3;
var foo={n:2}
console.log(foo.n)
})(foo)
console.log(foo.n)
這個聲明提升后是完全看不懂了,答案:1,2,3
var f={
1:0,
foo:function () {
console.log(i++)
}
}
不懂為什么不能訪問归粉,終于明白了應(yīng)該加上console.log(f.i++),我感覺我有點(diǎn)暈了椿疗,分不清對象和函數(shù)區(qū)別了。盏浇。变丧。
var i=5;
function foo() {
var i=0;
return function () {
console.log(i++)
}
}
var f1=foo();
var f2=foo();
f1();
f1();
f2();
這里訪問的i是內(nèi)部得i不是全局的i,做了這么多題還是有點(diǎn)不明白绢掰,痒蓬,童擎,,
var a=2;
var func=(function () {
var a=3;
return function () {
a++;
console.log(a)
}
})()
func();//4
func();//5這里訪問到的a是函數(shù)里面的
1 函數(shù)聲明和函數(shù)表達(dá)式有什么區(qū)別
- 函數(shù)聲明:function functionName(){}
- 函數(shù)表達(dá)式:var fn = function(){}
- 函數(shù)聲明會提前攻晒,函數(shù)表達(dá)式可以省略函數(shù)名顾复。輸出都是1,要好好想想寫寫看執(zhí)行順序
3 arguments 是什么(不知道在項(xiàng)目中實(shí)際用到哪鲁捏?)
- 可以在函數(shù)內(nèi)部通過使用 arguments 對象來獲取函數(shù)的所有參數(shù)芯砸。
- 這個對象為傳遞給函數(shù)的每個參數(shù)建立一個條目,條目的索引號從0開始给梅。它包括了函所要調(diào)用的參數(shù)
4 函數(shù)的"重載"怎樣實(shí)現(xiàn)
在JS中假丧,沒有重載。同名函數(shù)會覆蓋动羽。但可以在函數(shù)體針對不同的參數(shù)調(diào)用執(zhí)行相應(yīng)的邏輯
同學(xué)例子:
function printPeopleInfo(name, age, sex){
if(name){
console.log(name);
}
if(age){
console.log(age);
}
if(sex){
console.log(sex);
}
}
printPeopleInfo('Byron', 26);//Byron 26
printPeopleInfo('Byron', 26, 'male');//Byron 26 male
還有一個例子:
A()//輸出第二個A包帚,第二個把第一個覆蓋了
function A(){
alert('第一個A')
}
function A(){
alert('第二個A')
}
5 立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
(function(){
var a = 1;
})()
作用: 隔離作用域运吓。
7
function getInfo(name, age, sex){
console.log('name:',name);
console.log('age:', age);
console.log('sex:', sex);
console.log(arguments);
arguments[0] = 'valley';
console.log('name', name);
}
getInfo('饑人谷', 2, '男');
/*
name: 饑人谷
age: 2
sex: 男
['饑人谷',2,'男']
name valley
*/
getInfo('小谷', 3);
/*
name: 小谷
age: 3
sex: undefined
['小谷',3]
name valley
*/
getInfo('男');
/*
name: 男
age: undefined
sex: undefined
['男']
name valley
*/
9如下代碼的輸出渴邦?為什么
sayName('world');//hello world函數(shù)聲明前置
sayAge(10);//報(bào)錯,函數(shù)表達(dá)式不會前置拘哨。
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
10
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
};//打印出10
globalContext={
AO:{
x:10
foo:function
bar:function
}
Scope:null
}
foo.[[scope]]=globalContext.AO
bar.[[scope]]=globalContext.AO
1 X賦值為10谋梭,
2 執(zhí)行bar,發(fā)現(xiàn)全局AO里有bar,進(jìn)入bar執(zhí)行上下文
barContext={
AO:{
x:30
}
Scope:globalContext.AO
}
3先執(zhí)行x=30;
4執(zhí)行foo(),活動對象找不到foo倦青,去scope里面找發(fā)現(xiàn)存在瓮床,進(jìn)入foo上下文
fooContext={
AO:{
}
Scope:globalContext.AO
}
5 foo活動對象找不到x,去作用域里面找产镐,找到全局作用域發(fā)現(xiàn)X為10
11
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
globalContext={
AO:{
x:10
bar:function
}
Scope:null
}
bar.[[scope]]=globalContext.AO
1 X賦值為10纤垂,
2 執(zhí)行bar,發(fā)現(xiàn)全局AO里有bar,進(jìn)入bar執(zhí)行上下文
barContext={
AO:{
x:30
foo:function
}
Scope:globalContext.AO
}
foo.[[scope]]=barContext.AO
3先執(zhí)行x=30;
4執(zhí)行foo(),活動對象有foo磷账,進(jìn)入foo上下文
fooContext={
AO:{
}
Scope:barContext.AO
}
5 foo活動對象找不到x,去作用域里面找贾虽,找到bar作用域發(fā)現(xiàn)X為30
13
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
globalContext = {
AO:{
x:10
bar:function
},
Scope:null
}
bar.[[scope]] = globalContext.AO
2.調(diào)用bar()
barContext = {
AO:{
x:30
function
},
Scope:globalContext.AO
}
function.[[scope]] = barContext.AO
3.調(diào)用立即執(zhí)行函數(shù)
functionContext = {
AO:{},
Scope:function.[[scope]]//barContext.AO
}
結(jié)果為30*/
14
題目var a = 1;
function fn(){
console.log(a)
var a = 5
console.log(a)
a++
var a
fn3()
fn2()
console.log(a)
function fn2(){
console.log(a)
a = 20
}
}
function fn3(){
console.log(a)
a = 200
}
fn()
console.log(a)
解答 globalContext={
AO:{
a:1-->后來變?yōu)?00
fn:function
fn3:function
}
Scope:null
}
fn.[[scope]]=globalContext.AO
fn3.[[scope]]=globalContext.AO
1 a賦值為1逃糟,
2 執(zhí)行fn,發(fā)現(xiàn)全局AO里有fn,進(jìn)入fn執(zhí)行上下文
fnContext={
AO:{
a:undefined->5->6->20
fn2:function
}
Scope:globalContext.AO
}
fn2.[[scope]]=fnContext.AO
3 先執(zhí)行打印a,為undefined;
4 a賦值為5
5 再次打印a為5
6 a++變?yōu)?
7 執(zhí)行fn3,fn2活動對象沒有蓬豁,去找fn2作用域發(fā)現(xiàn)全局對象里有绰咽,進(jìn)入fn3上下文
fn3Context={
AO:{
}
Scope:globalContext.AO
}
8 打印a,fn3活動對象沒有,去全局里面找地粪,發(fā)現(xiàn)全局里面的a為1取募,打印1
9 a賦值為200,全局對象里a變?yōu)?00
10執(zhí)行fn2蟆技,進(jìn)入fn2上下文
fn2Context={
AO:{
}
Scope:fnContext.AO
}
11打印a玩敏,自己沒有去fn里面找,發(fā)現(xiàn)a已經(jīng)變?yōu)?
12a賦值為20斗忌,那fn里面a變?yōu)?0
13打印a,a賦值為20
14 跳到全局下,打印a,為200
所以最后打印順序是:undefined 5 1 6 20 200(發(fā)現(xiàn)不用寫這個也可以看出a是多少了)