Can the arguments property have access to this.some_function.arguments? I really can not explain?

I read a JavaScript book, and I read how you can extend the functionality of arrays of JavaScript array with a prototype, then I came to this example, which I could not understand, and there was no deep explanation for this, and I do not know who can understand this:

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 managed to access the arguments, but I don’t know how it works, that means it refers to the object we are calling this method (array in this context), and then some_function refer to the property of this object means the function that we we implement, but then the arguments are not a static property of this function, then how does it work? it only works in the context of the call, Iam cannot use this for other ex: -

 this.some_other_function.arguments // gives error 
+5
source share
3 answers

This seems to be an outdated JavaScript feature. See the MDN documentation : Function.arguments , which states:

Outdated

This feature has been removed from web standards. Although some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Used pages or web applications can be interrupted at any time.

The behavior is unexpected. This may be the reason why it is out of date. As you said, a function is a static object, so how can it have dynamic arguments ? Well, I think there is no other explanation than the obvious: the JavaScript engine assigns the arguments function before each call.

By the way, there is a simpler code that demonstrates the same thing:

 function f() { return f.arguments; } f(8); // returns [8] f(8, 9); // returns [8, 9] f.arguments === null // true 

The solution, of course, uses the local variable arguments instead of func.arguments , see MDN: arguments .

+4
source

The Arguments object is available to all functions as a local variable.
You can use the object argument inside the function, but you cannot access them using instance.arguments.
Accessing the arguments to the .arguments function is a strange function provided by javascript that is currently deprecated.
Basically, when you do function.arguments, it gives an argument variable to the function currently executing on the stack. Try going through my code and reading the console .. for 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); 
+1
source

The basic JavaScript concept is "Everything is just an object," so an array is an object with a prototype and constructor.

Any function declared as a property for an object is called a method, the argument is the parameter that you pass inside the function to 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 the prototype area, which means that you cannot access the properties of the else method, where unless you pass the method itself or its constructor to this other area. p>

You can refer to a book called JavaScript written by cody lindley, this needs some additional JavaScript knowledge.

By MDN

This feature has been removed from web standards. Although some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Used pages or web applications can be interrupted at any time.

+1
source

Source: https://habr.com/ru/post/1239197/


All Articles