A small question about Js's new operator

Source: Internet
Author: User

today, There is a problem, I feel there is a certain confusion. And this is a point, sometimes the formation of inertia thinking is very deadly, especially when encountering small knowledge points. Not much to say, first look at this problem:

After running this code, what is the value of "mike"?

function person (name, age) {       this. name = name;         this. age = parseint (age,10);      } var mike = person (' Mike ', ' 25 ');

A. NULL

B. code cannot run, syntax error

C. {name: ' Mike ', age:25}

D. undefined

E. {name: ' Mike ', age: ' 25 '}

I'll take a look at this Question. Did not hesitate to choose c, the result is wrong. The reason for the error is obvious: mistakenly put the last sentence var Mike = person (...); As an instantiation of the person class. And no new operator was found.

What does it mean to have a new operator? We know that in javascript, when we implement object-oriented, there is no concept of "class", but we use OOP in C++/java to understand it.

In js, use the Function keyword to define "class", so the "class" here is essentially a function, the function is the Object. I have to say here, how to distinguish between the definition of a "class" or a normal function? Usually the programming habit

When a class is defined, the class name is capitalized, and the normal function is defined in Lowercase. second, look at the behavior of the inner function body.

It is obvious that you are defining a "person" class. But don't forget, it's still a Function. So at the end of the call, when the new operator is not added, it is called as a normal Function. is actually a very simple statement. But before and after the contrast of thinking is a bit

Can't turn the bend (or maybe I'm a rookie without turning the bend ^_^). and return to the function to consider person (), which is a function without a return Value. We also know that when the function of JS does not have a return statement, the default return is Undefined.

So......... var Mike = undefined. correct answer select D.

The following is the original address of the new operator: http://blog.csdn.net/motian06/article/details/8258942

new operator

With the introduction of the underlying concept above, and with the new operator, we are able to create objects in the way that traditional object-oriented class + new is created, and in JavaScript we make this kind of a pseudoclassical.
Based on the above example, we execute the following code

var obj  new Base ();

What is the result of this code, the object model we see in the JavaScript engine is:

What exactly did the new operator do? It's really simple, and it's done three things.

var obj  == base.prototype; Base.call (obj);

In the first line, we created an empty object, obj.
In the second line, we point the __proto__ member of this empty object to the base function object prototype member object
In the third line, we replace the this pointer of the base function object with obj and then call the base function, so we assign an ID member variable to the obj object, the value of this member variable is "base", for the use of a call function, see Chenhao "Javascript Object-oriented Programming article


What happens if we add some functions to the Base.prototype object?
For example, the following code:

function () {    returnthis. id;}

So when we create a new object using new, The ToString method can also be accessed as a method of the new object, depending on the __proto__ feature. So we see:
constructor, Let's set the ' class ' member variable (for example, the ID in the example), construct the child object prototype, we'll Set the public method of ' class '. thus, the effects of class and class instantiation are simulated through function objects and javascript-specific __proto__ and prototype members and the new Operator.

A small question about Js's new operator

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.