I was reading a JavaScript book and I was reading how you can extend the arrays functionality of JavaScript array by prototype, then I came to this example that I could not understand, and there was no deep explanation of it, and Iam not able to understand that:
Array.prototype.some_function = function(){
var args = this.some_function.arguments; // 1
var args_length = this.some_function.arguments.length; // 2
...
} // some_function
here I was able to access the arguments, but i don't know how this is working, mean this refer to the object on which we are calling this method (array in this context), then some_function refer to the property of that object, mean to the function we are implementing, but then arguments is not the static property of that function then how is it working ? it only works in calling context, Iam not able to use this for others ex:-
this.some_other_function.arguments // gives error
It seems that this is a deprecated feature of JavaScript. See the documentation on MDN: Function.arguments which says:
Deprecated
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
The behaviour is in deed unexpected. That may be the reason why it is deprecated. As you said, the function is a static object, so how can it have dynamic arguments
? Well, I guess there is no other explanation than the obvious one: JavaScript engine assigns the arguments
value to the function before each call.
BTW, there is a simpler code which demonstrates the same:
function f() { return f.arguments; }
f(8); // returns [8]
f(8, 9); // returns [8, 9]
f.arguments === null // true
The solution is, of course, use local variable arguments
instead of func.arguments
, see MDN: arguments.
JavaScript main concept "Everything is just an object" ,so an array is an object that has a prototype , and a constructor.
Any function declared as a property for an object is called a method , an argument is the parameter you pass inside a function for ex:
function Add(a,b)
{
console.log(this.arguments) // logs an array-like of [a,b]
return a+b;
}
Where is the method defined ? Only inside prototype scope , which means you can't access method properties else where , unless you pass the method itself , or its constructor to that other scope.
You can refer to a book called JavaScript succinctly by cody lindley , this
needs some advanced knowledge of JavaScript.
As per MDN
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
Arguments object is available for all functions as local variable.
You can use arguments object inside function but you cannot access them by instance.arguments.
Accessing arguments by function.arguments is a strange feature provided by javascript which has been deprecated as of now.
Basically when you do function.arguments it gives the arguments variable of the function currently executing in stack.
Try to go through my code and read the consoles.. for more clarity
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With