这怎么可能没有实例(新'ing')是:VAR一个= Array.prototype.slice.call(参数)? [英] How is this possible without instantiating (new-'ing') it: var a = Array.prototype.slice.call(arguments)?
问题描述
在的函数内的情况下,这里的code(基于制作功能的论据到一个数组的标准模式):
In the context of inside a function, here's the code (based on the standard pattern of making a function's 'arguments' into an array):
变参= Array.prototype.slice.call(参数);
我想学习这一点(我在JavaScript的初学者,从C#来)。
I'm trying to study this out (am a beginner at JavaScript, coming from C#).
据我所知,片
是一个实例方法,由于它是一个阵的原型
功能。
I understand that slice
is an instance method due to it being a prototype
function of Array.
我也明白,这是不是一个静态的效用函数,这意味着使用它,你必须新
起来像这样:(例子) myarray的无功=新的Array(); myArray.slice(...);
I also understand that this is not a static 'utility function', meaning to use it, you have to new
it up like so: (example) var myArray = new Array(); myArray.slice(...);
呼叫
传递一个对象在这里上下文更改为的参数
call
passes an object in here to change the context to that of arguments
与此相关的,我不知道还之间的区别 Array.prototype.slice.call([32,32,121,412])
和 Array.prototype.slice([32,32,121,412])
不是调用的上下文
。
Related to this, I don't also know the difference between
Array.prototype.slice.call([32,32,121,412])
and Array.prototype.slice([32,32,121,412])
not in the context of call
.
所以,这里是我的问题:
我只是不明白这是如何工作有关情况VS静态方法...所以任何人都可以解释变参= Array.prototype.slice.call(参数)的复杂性;
?
I just don't get how this works in relation to instance vs static methods... so can anyone explain the intricacies of var args = Array.prototype.slice.call(arguments);
?
为什么可以这样无需调用使用新
?
Why can this be used without calling new
?
为什么这可能吗?这不是一个静态方法,它必须是'newed'了,当你使用呼叫
函数... (至少在我的C#的心态,它只能...)的
Why was this possible? It's not a Static method, and it must be 'newed' up, and it only works when you use call
function... (at least in my C# mentality...)
推荐答案
与C#/ Java的功能实际上是一等公民在JavaScript中。
Unlike C#/Java, functions are actually first-class citizens in JavaScript.
在一般意义上:
这意味着没有必要新起来..它的工作原理正是因为它是因为它是自己的对象。
This means there is no need to "new" it up.. it works exactly as it is because it is its own object.
新ING一个功能(在一般意义上)仅改变该功能将被分配给该变量的这个值,然后返回。
"new"ing up a function (in a general sense) merely changes the 'this' value of that function to the variable it will be assigned to, then returns it.
有可能使用一个JavaScript函数没有newing起来,因为他们是一等公民在JavaScript中。
It is possible to use a function in JavaScript without "newing" it up because they are "first-class citizens" in JavaScript.
在一个更深入的意义上说,这是新的作用:
- 它创建一个新的对象从MyConstructor.prototype派生
- 它的构造函数的'这个'值分配给新的对象
- 执行code里面(增加属性的新对象/实例)
- 返回新对象
In a more in-depth sense, this is what "new" does:
- it creates a new object deriving from MyConstructor.prototype
- it assigns the 'this' value of the constructor function to the new object
- execute the code inside (adds properties to new object/instance)
- returns the new object
关于我从实例学到了一些额外的注意事项:
- 他们没有一个.prototype财产像他们的构造函数
- 虽然拥有[[原型]]属性,从MyConstructor.prototype派生
- 覆盖在属性中的实例的阴影MyConstructor.prototype [属性] ..
Some extra notes about what I learned from instances:
- they don't have a .prototype property like their constructor functions
- though they have a [[prototype]] property, derived from MyConstructor.prototype
- overriding a property in the instance shadows the MyConstructor.prototype[property]..
这篇关于这怎么可能没有实例(新'ing')是:VAR一个= Array.prototype.slice.call(参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!