Parse invisible data types in JavaScript _ javascript skills

Source: Internet
Author: User
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.

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.