This article mainly introduces the invisible data types in JavaScript in detail. For more information, see, I hope to help you. JS provides some built-in objects, functions, and constructors for programming, such as Math, parseInt, Object, and Array. These are all visible and can be used for programming. For example, I can use new Object or new Array.
Some objects are invisible. These objects can only be provided by the engine in special circumstances. These types of objects are often removed from some features. Below are some examples
1. Arguments type
The Arguments type cannot be manually created by the programmer, that is, you cannot create new Arguments (). It has only one arguments object
The Code is as follows:
Function func (){
Console. log (arguments [0]) // 1
Console. log (arguments. length) // 3
}
Func (1, 2, 3)
The arguments object is created when a function is called and is visible and used only within the function. As you can see, arguments is very similar to Array. It can take elements by index, and it also has the length attribute. But it is not an Array, and it does not have some methods of Array, such as push and pop. Arguments is defined in ES5 10.6.
Ii. Functions returned by bind are very special.
Bind is a new method that ES5 adds to Function. prototype. It is called directly on function like call/apply. It returns a function that specifies the context and parameters.
The Code is as follows:
Function func (age ){
Console. log ('name: '+ this. name +', career: '+ age)
}
Var person = {name: 'John Carthy '}
Var f1 = func. bind (person, 'computer scientist ')
F1 () // name: John McCarthy, career: computer scientist
We can see that the returned function f1 is called using parentheses like a common function. Everything works, but the following code will surprise you
The Code is as follows:
Function func (age ){
Console. log ('name: '+ this. name +', career: '+ age)
}
Var person = {name: 'John Carthy '}
Var f1 = func. bind (person, 'computer scientist ')
Console. log (f1.prototype) // undefined
Compared with the above Code, the last sentence is different. Instead of executing f1 (), f1.prototype is printed and undefined is found.
Strange? Doesn't each function have a prototype attribute, which is used to implement prototype inheritance. Indeed, the function returned by bind is special, and it does not have prototype. This type of special function is created by the JS engine, and the client programmer cannot directly obtain it through function declaration or function.
This clearly indicates ES5 15.3.4.5 in the specification.