Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arguements是什么? #2

Open
jadenlwangzai opened this issue Mar 7, 2019 · 0 comments
Open

arguements是什么? #2

jadenlwangzai opened this issue Mar 7, 2019 · 0 comments
Labels
JavaScript JavaScript Knowledge

Comments

@jadenlwangzai
Copy link
Owner

问题

var length = 10;
function fn(){
    alert(this.length)
}
var obj = {
    length: 5,
    method: function(fn) {
    arguments[0]()
     }
}
obj.method(fn);//1

这段代码中的arguments[0]()是第一个参数?带一对小括号是什么意思?

理解

我们可以先从最后调用obj.method(fn)开始理解。

1.obj是对象,method()obj的方法,fnmethod()的参数,fn是函数的名, 他引用对应的函数。arguments是javascript的一个内置对象

An Array-like object corresponding to the arguments passed to a function.
The arguments object is a local variable available within all functions; arguments as a property of Function can no longer be used.
Description:You can refer to a function‘s arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry’s index starting at 0.

2.arguments是用来取得method(fn)函数的参数数组,在这里也就是fn,即arguments[0]===fn。所以arguments[0]()就等于fn()

妈蛋,是不是到这里要开始风中凌乱了,this.length究竟是指向那个对象呢?
可以这样理解:

var arguments={
     fn:function(){
     alert(this.length)
  },

或者更可以这样理解:

arguments = {
 0: fn, //也就是 functon() {alert(this.length)} 
 1: 第二个参数, //没有 
 2: 第三个参数, //没有
 ..., 
 length: 1 //只有一个参数
}

最后,这个1就是arguments.length,也就是本函数参数的个数。

@jadenlwangzai jadenlwangzai added the JavaScript JavaScript Knowledge label Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript JavaScript Knowledge
Projects
None yet
Development

No branches or pull requests

1 participant