函數(shù)聲明和函數(shù)表達(dá)式
函數(shù)聲明:function functionName(){}
??函數(shù)表達(dá)式:var fn = function(){}
??函數(shù)聲明會(huì)提前,函數(shù)表達(dá)式可以省略標(biāo)識(shí)符。函數(shù)表達(dá)式不會(huì)聲明前置,應(yīng)該注意位置。
變量的聲明前置及函數(shù)的聲明前置
所謂的變量聲明前置就是在一個(gè)作用域塊中,所有的變量都被放在塊的開始處聲明。
函數(shù)的聲明前置和變量聲明前置一樣,執(zhí)行代碼之前會(huì)先讀取函數(shù)聲明,只要函數(shù)在代碼中進(jìn)行了聲明,無論它在哪個(gè)位置上進(jìn)行聲明,js引擎都會(huì)將它的聲明放在范圍作用域的頂部。
arguments
arguments是一個(gè)類數(shù)組對(duì)象。代表傳給一個(gè)function的參數(shù)列表。
arguments對(duì)象是函數(shù)內(nèi)部的本地變量;arguments 已經(jīng)不再是函數(shù)的屬性了。可以在函數(shù)內(nèi)部通過使用 arguments 對(duì)象來獲取函數(shù)的所有參數(shù)。這個(gè)對(duì)象為傳遞給函數(shù)的每個(gè)參數(shù)建立一個(gè)條目,條目的索引號(hào)從0開始。它包括了函所要調(diào)用的參數(shù)。object對(duì)象。類數(shù)組。
函數(shù)的"重載"怎樣實(shí)現(xiàn)
在JS中,沒有重載。同名函數(shù)會(huì)覆蓋??梢允褂胊rguments.length來判斷傳入?yún)?shù)的個(gè)數(shù),針對(duì)不同參數(shù)采取不同方法實(shí)現(xiàn)重載的功能。
立即執(zhí)行函數(shù)表達(dá)式是什么?有什么作用
(function(){var a = 1;})()
javascript中沒用私有作用域的概念,如果在多人開發(fā)的項(xiàng)目上,你在全局或局部作用域中聲明了一些變量,可能會(huì)被其他人不小心用同名的變量給覆蓋掉,根據(jù)javascript函數(shù)作用域鏈的特性,可以使用這種技術(shù)可以模仿一個(gè)私有作用域,用匿名函數(shù)作為一個(gè)“容器”,“容器”內(nèi)部可以訪問外部的變量,而外部環(huán)境不能訪問“容器”內(nèi)部的變量,所以( function(){…} )()內(nèi)部定義的變量不會(huì)和外部的變量發(fā)生沖突,俗稱“匿名包裹器”或“命名空間”。
求n!,用遞歸來實(shí)現(xiàn)
function multiply(n){
if(n===1 || n<=0){
return 1;
}else
return n*multiply(n-1);
}
console.log(multiply(n))
以下代碼輸出什么?
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
*/
寫一個(gè)函數(shù),返回參數(shù)的平方和?
function sumOfSquares(){
var sum=0;
for(var i=0;i<arguments.length;i++){
sum=sum+arguments[i]*arguments[i];
}
console.log(sum);
}
var result = sumOfSquares(2,3,4)
var result2 = sumOfSquares(1,3)
console.log(result) //29
console.log(result) //10
如下代碼的輸出?為什么
console.log(a);//undefined 聲明前置但賦值在后
var a = 1;
console.log(b);//報(bào)錯(cuò),b沒有聲明
如下代碼的輸出?為什么
sayName('world');//輸出hello world,函數(shù)聲明前置
sayAge(10);//報(bào)錯(cuò),函數(shù)表達(dá)式不會(huì)前置
function sayName(name){
console.log('hello ', name);
}
var sayAge = function(age){
console.log(age);
};
如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10
bar()
function foo() {
console.log(x)
}
function bar(){
var x = 30
foo()
}
/* 1.
globalContext = {
AO:{
x:10
foo:function
bar:function
},
Scope:null
}
foo.[[scope]] = globalContext.AO
bar.[[scope]] = globalContext.AO
2.調(diào)用bar()
barContext = {
AO:{
x:30
},
Scope:bar.[[scope]] = globalContext.AO
}
x變成30
3.調(diào)用foo()
fooContext = {
AO:{},
Scope:foo.[[scope]] = globalContext.AO
}
輸出10
*/
如下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
function foo(){
console.log(x)
}
foo();
}
/* 1.
globalContext = {
AO:{
x:10
bar:function
},
Scope:null
}
bar.[[scope]] = globalContext.AO
2.調(diào)用bar()
barContext = {
AO:{
x:30
},
Scope:bar.[[scope]] // globalContext.AO
}
foo.[[scope]] = barContext.AO
3.調(diào)用foo()
fooContext = {
AO:{},
scope:foo.[[scope]]
}
輸出結(jié)果為30
*/
以下代碼輸出什么? 寫出作用域鏈的查找過程偽代碼
var x = 10;
bar()
function bar(){
var x = 30;
(function (){
console.log(x)
})()
}
/*1.
globalContext = {
AO:{
x:10
bar:function
},
Scope:null
}
bar.[[scope]] = globalContext.AO
2.調(diào)用bar()
barContext = {
AO:{
x:30
function
},
Scope:bar.[[scope]] //globalContext.AO
}
function.[[scope]] = barContext.AO
3.調(diào)用立即執(zhí)行函數(shù)
functionContext = {
AO:{},
Scope:function.[[scope]]//barContext.AO
}
結(jié)果為30*/
以下代碼輸出什么? 寫出作用域鏈查找過程偽代碼
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)
/*
1.
globalContext = {
AO:{
a:1
fn:function
fn3:function
},
Scope:null
}
fn.[[scope]] = globalContext.AO
fn3.[[scope]] = globalContext.AO
2.調(diào)用fn()
fnContext = {
AO:{
a:undefined
fn2:function
},
Scope:fn.[[scope]] // globalContext.AO
}
fn2.[[scope]] = fnContext.AO
3.
fn3Context = {
AO:{
a:200
},
Scope:fn3Context.[[scope]]//globalContext.AO
}
fn2ConText = {
AO:{
a:20
},
Scope:fn2ConText.[[scope]]//fnContext.AO
}
//輸出的結(jié)果 undefined 5 1 6 20 200
*/