Type casting

Good afternoon, I'm new to the JavaScript world, and I have a question with a simple example,

function myFun(){
    return arguments;
}

var myVar=myFun("a","b","c"); 

//1
console.log(myVar); //=>//["a", "b", "c", callee: function, Symbol(Symbol.iterator): function]

//2
for(m in myVar){
    console.log(m); //will generate only the realistic arguments "a","b","c".
}

According to the above snippet, why does the first call invoke an argument object with inherited properties from the main Function object, and the second only generates realistic arguments.

If we passed an argument to the object of the second function, why should it be passed only with realistic data, for example

function foo(something) {
console.log(something); ///=>this will generate number 3 as parameter only, not the rest of the object
 return this.a + something;
}
var obj = {
 a: 2
};
var bar = function() {
 return foo.apply( obj, arguments );
};
var b = bar( 3 ); // 2 3

as indicated in the console.log line (something), it will only generate realistic parameters only

+4
source share
2 answers

The error in the following example is what is interesting:

function foo() { }

foo.apply(null, 5);
Run codeHide result

The error says (on google chrome):

CreateListFromArrayLike called for a non-object.

, , apply , CreateListFromArrayLike ( ) , , , .

: mozilla firefox , , apply.

+1

@ibrahim mahrir, this , , apply , ,

  function toArray(args) {
    return Array.prototype.slice.call(args);
}

function firstFun(){
   console.log(toArray(arguments));
}
firstFun("A","b");

/// , , @ibrahim, .

+1

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


All Articles