Parsing invisible data types in JavaScript _javascript tips

Source: Internet
Author: User

JS provides some built-in objects, functions, and constructors for programming, such as math, parseint, object, array, and so on. These are visible and can be used in programming. For example, I can new Object or new Array.

Some are not visible, and these objects can only be provided by the engine in a particular situation. These types of objects are often reduced to a number of functions. Listed below are some

I. Type of Arguments
The Arguments type cannot be manually created by the programmer, that is, you cannot create a new Arguments (). It has and has only one object arguments

Copy Code code as follows:

function func () {
Console.log (Arguments[0])//1
Console.log (Arguments.length)//3
}
Func (1, 2, 3)

The arguments object is created at the time of the function call and is visible and used only within the function. You can see that arguments is very much like an array, you can index elements and the length property. But it is not array, it has no array of some methods such as push, pop and so on. Arguments is defined in ES5 10.6.

Second, the function returned by BIND is very special
Bind is a new method that ES5 gives Function.prototype, which is called directly on function like call/apply. It returns a function that specifies the context and parameters.

Copy 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 the same as the normal function using parentheses to execute the call. Everything's fine, but the code below will surprise you.
Copy 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

Compared with the above code, the last sentence is different, does not execute F1 (), but print out f1.prototype, found to be undefined.

Is it strange? Does each function have a prototype attribute, which is used to implement the prototype inheritance Oh. Indeed, the function returned by bind is rather special, and it has no prototype. This special function is created by the JS engine, and the client programmer is not able to use the function declaration or the direct amount of the function.

This point is clearly indicated in the specification ES5 15.3.4.5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.