JS provides some built-in objects, functions and constructors for us to program, such as Math, parseInt, Object, Array, etc. These are all visible and can be used during programming. For example, I can use new Object or new Array.
Some are invisible and can only be provided by the engine in special circumstances. These types of objects often have reduced functionality. Here are some
1. Arguments type
The Arguments type cannot have its objects manually created by the programmer, i.e. you cannot new Arguments(). It has and only one object arguments
Copy the code code as follows:
function func() {
console.log(arguments[0]) // 1
console.log(arguments.length) // 3
}
fun(1, 2, 3)
The arguments object is created when the function is called and is only visible and used inside the function. You can see that arguments are very similar to Array, elements can be retrieved by index, and they also have a length attribute. But it is not Array. It does not have some methods of Array such as push, pop, etc. Arguments are defined in ES5 10.6.
2. The function returned by bind is very special.
bind is a new method added by ES5 to Function.prototype. It is called directly on the function like call/apply. It returns a function with specified context and parameters.
Copy the code code as follows:
function func(age) {
console.log('name: ' + this.name + ', career: ' + age)
}
var person = {name: 'John McCarthy'}
var f1 = func.bind(person, 'computer scientist')
f1() // name: John McCarthy, career: computer scientist
You can see that the returned function f1 is called using parentheses like a normal function. Everything works fine, but the following code will surprise you
Copy the code code as follows:
function func(age) {
console.log('name: ' + this.name + ', career: ' + age)
}
var person = {name: 'John McCarthy'}
var f1 = func.bind(person, 'computer scientist')
console.log(f1.prototype) // undefined
Comparing with the above code, the last sentence is different. f1() is not executed, but f1.prototype is printed out and found to be undefined.
Weird? Doesn't every function have a prototype attribute? This is used to implement prototype inheritance. Indeed, the function returned by bind is special, it does not have a prototype. This special function is created by the JS engine and cannot be directly measured by the client programmer through function declaration or function.
This is clearly stated in the specification ES5 15.3.4.5