arguments属性可以由this访问.some_function.arguments?实际上我无法解释
arguments property can be access by this.some_function.arguments ? actually i m not able to explain?
我正在读一本JavaScript书,我正在读如何通过原型扩展JavaScript数组的数组功能,然后我遇到了这个我无法理解的例子,对此没有深入的解释,我也无法理解:
Array.prototype.some_function = function(){
var args = this.some_function.arguments; // 1
var args_length = this.some_function.arguments.length; // 2
...
} // some_function
在这里,我可以访问参数,但我不知道这是如何工作的,意味着this指的是我们正在调用该方法的对象(上下文中的数组),然后some_function指的是该对象的属性,意味着我们正在实现的函数,但参数不是该函数的静态属性,那么它是如何工作?它只在调用上下文中工作,我不能将其用于其他人,例如:-
this.some_other_function.arguments // gives error
这似乎是JavaScript的一个不受欢迎的特性。请参阅MDN:Function.arguments上的文档,其中写道:
已弃用
此功能已从Web标准中删除。尽管有些浏览器可能仍然支持它,它正在被删除。不要在旧项目或新项目中使用它。使用它的页面或Web应用程序可能随时休息。
这种行为实际上出乎意料。这可能是它被弃用的原因。正如你所说,函数是一个静态对象,那么它怎么可能有动态arguments
呢?好吧,我想除了显而易见的解释之外,没有其他解释了:JavaScript引擎在每次调用之前为函数分配arguments
值。
顺便说一句,有一个更简单的代码可以证明这一点:
function f() { return f.arguments; }
f(8); // returns [8]
f(8, 9); // returns [8, 9]
f.arguments === null // true
当然,解决方案是使用局部变量arguments
而不是func.arguments
,请参阅MDN:arguments。
JavaScript的主要概念"一切都只是一个对象",所以数组是一个有原型和构造函数的对象。
任何被声明为对象属性的函数都被称为方法,参数是您在函数内部传递的参数,例如:
function Add(a,b)
{
console.log(this.arguments) // logs an array-like of [a,b]
return a+b;
}
方法在哪里定义仅在原型作用域内,这意味着除非将方法本身或其构造函数传递到其他作用域,否则不能访问其他位置的方法属性。
你可以简单地参考cody lindley的一本名为JavaScript的书,this
需要一些高级的JavaScript知识。
根据MDN
此功能已从Web标准中删除。尽管有些浏览器可能仍然支持它,它正在被删除。不要在旧项目或新项目中使用它。使用它的页面或Web应用程序可能随时休息。
Arguments对象作为局部变量可用于所有函数
您可以在函数中使用arguments对象,但不能通过实例访问它们。arguments。
通过函数访问参数。参数是javascript提供的一个奇怪功能,目前已被弃用
基本上,当你执行函数.arguments时,它会给出当前在堆栈中执行的函数的arguments变量。试着浏览我的代码并阅读控制台。。为了提高的清晰度
var someContructor = function() {
console.log('someContructors arguments');
console.log(arguments);
//SomeConstructor is currently in stack executing
//accessing its arguments by function.arguments
console.log('someContructors arguments accesed using function.arguments');
console.log(someContructor.arguments);
};
someContructor.prototype.method = function() {
console.log('method arguments');
console.log(arguments);
//method is in stack executing
//accessing its arguments by function.arguments
console.log('method arguments accesed using function.arguments');
console.log(this.method.arguments);
};
var newObject = new someContructor(1, 2);
newObject.method(1, 2, 12, 23, 23, 232, 3);
- 直接在函数声明上使用function.prototype.bind
- jQuery:.click(function(){(element),collapse('show',f
- 如果连接类型为none,则Javascript Function Only警报
- onLoad function phonegap android
- reducers在redux中得到Function not Object,what'it’他错了
- WIll window.addEventListener(“加载”,function(),false);等待浏览器自动填
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- 理解AngularJS函数$arguments
- Javascript onunload function
- 在init function()中调用getElementById(“..”),而不是想用它做点什么的函数
- spyOn:应为间谍,但得到了Function
- (typeof variable === “function”) 和 jQuery.isFunction() 有什么区别
- this内部的值返回this.each(function(){})
- 如何在JavaScript中创建类似于something.function()的函数
- 如何通过 Function.prototype.apply() 更改调用函数的参数
- 为什么会失败: 'function log(){console.log.apply(null,arguments);}`
- 获取被调用方之一的 Function.Arguments.Arguments
- Javascript function arguments with "-"
- Node.js中的Function Arguments对象与Chrome' js中的JavaScript不同
- 未捕获的类型错误:Object function (){h.apply(this,arguments)}没有方法'