Comparison of JS object and new Object/function

Source: Internet
Author: User
Tags lowercase wrapper

Everything is the object

In the JavaScript world, everything is the object. In addition to null and undefined, other basic type numbers, strings, and Boolean values have a wrapper object on them. A feature of an object is that you can call the method directly on it. For a numeric base type, attempting to invoke the ToString method on it will fail, but the call will not fail when enclosed in parentheses, and the internal implementation is to convert the base type to an object with the appropriate wrapper object. So (1). ToString () is the equivalent of new number (1). ToString (). Therefore, you can really use the basic type numbers, strings, Boolean, etc. as objects, just pay attention to the syntax to be appropriate.

At the same time, we notice that the numbers in JavaScript are not floating-point and plastic, all numbers are floating-point types, just omit the decimal point, for example, you see 1 can be written as 1. And that's why when you try to 1.toString (), you get an error. So the correct way of writing should be this: 1. ToString (), or, as described above, with parentheses, where the parentheses function is to correct the JS parser, and do not take the point at the back of the 1 as a decimal. The internal implementation, as described above, is to 1. Use the wrapper object to convert to an object and then call the method.

Using new to invoke it as a constructor creates an instance object of that class, which is the name of the function in which the middle uses This.propertyname=value to assign values to the properties of the instance object, and returns the object regardless of return.

And if the direct call is to simply execute the code inside, do not create the instance object, this refers to the invocation environment under the This object, if it is called directly in the global, is window, there is no default return value.

You want to create a class, generally using the former (constructor pattern), and of course you can use the latter, such as the factory pattern and the parasitic constructor pattern.
Constructor mode:

The code is as follows Copy Code

function Person (name,age,job) {
This.name=name;
This.age=age;
This.job=job;
This.introduce=function () {
Alert ("My name is" +this.name+ ", I Am" +age+ "year (s) old, I am a" +job+ ");
}
}

Factory mode:

The code is as follows Copy Code

function Createperson (name,age,job) {
var o=new Object ();
O.name=name;
O.age=age;
O.job=job;
return o;
}

Parasitic constructor mode:

The code is as follows Copy Code

function Specialarray () {
var values=new Array ();
Values.push.apply (values,arguments);
Values.topipedstring=function () {
Return This.join ("|");
};
return values;
}

The disadvantage of the latter two methods is that the actual type cannot be detected by instanceof because the object type is returned.


The following example illustrates:

A JavaScript function can invoke a constructor via the new operator, such as New Chenqiguo (). As far as its definition is concerned, there is no difference from ordinary functions. The difference is that when a function calls the new operator, JavaScript provides it with a prototype object, and when the function is used as a constructor to build a new object, its internal [[prototype]] property becomes a reference to the object being constructed

What do you mean by that, we'll look at the following code

The code is as follows Copy Code

function Chenqiguo () {
Alert (' Qiguo ');
}
Chenqiguo () and new Chenqiguo () are the same, will print Qiguo this string, and the code into the following way:
function Chenqiguo () {
Alert (' Qiguo ');
}
Chenqiguo.prototype.getName = function () {
Alert (' prototype Qiguo ');
}

We instantiate the constructor using new and perform a prototype evaluation with a normal function.

The code is as follows Copy Code

var Chenqiguo = new Chenqiguo ();//Print Qiguo
Chenqiguo.getname (); Print prototype name
var name = Chenqiguo (); Print Qiguo
Name.getname (); Will error, name is undefined

You can see that when we create an object with the new operator, we can print the value on the prototype, when you use a regular function, it doesn't, which explains that when we create a new object with a function as a constructor, its internal prototype property becomes a reference to the object being created.

Put aside the language level to observe functions and constructors, the name of the constructor is usually used in the form of a first-letter capitalization to indicate that it is a constructor, and that it is a normal function in lowercase. As a developer of JavaScript, these two situations should be strictly distinguished, If the constructor is capitalized, the first letter is lowercase

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.