JavaScript new constructor and object detailed

Source: Internet
Author: User

First we disassemble to the following code
JScript code

  code is as follows copy code
var str = function () {
                 return to ' Hello world ';
           }
            alert (New Str ());
            var str1 = function () {
                 return to New String (' Hello World ');
           }
            Alert (new str1 ())

This code and your previous code is a meaning, understand it.
Okay, let's do the experiment.
JScript Code

  code is as follows copy code
var str = function () {
                 return to ' Hello world ';
           }
            alert (str);
            var str1 = function () {
                 return to New String (' Hello World ');
           }
            alert (str1)


The printed result is expected. It's also obvious, not much explained.
JScript code           

  code is as follows copy code
var str = function () {
                 return to ' Hello world ';
           }
            alert (str ());
            var str1 = function () {
                 return to New String (' Hello World ');
           }
            alert (str1 ())

Well, coming down here with the meaning, the results are understandable. But why, the result of a function as a builder is different.
JScript Code

The code is as follows Copy Code

             function str () {
                 return to ' Hello world ';
           }

            function str1 () {
                 return to New String (' Hello World ');
           }
            alert (New Str ());
            Alert (new str1 ());


We need to know 2 things to solve this problem.
1, new () Dry God horse thing
Many child shoes know that the new function () returns the this pointer object as an object reference. But if there is no this in the demo above, you return a value directly.

Let's look at a new example
JScript Code

The code is as follows Copy Code
var test = ' Hello World ';
var test1 = new String (' Hello World ');

function Teststr () {
return test;
}
function TestStr1 () {
return test1;
}

Alert (new teststr () = = test);//outputs:false
Alert (new testStr1 () = = test1); output:true


Yes, it's a bit of a feeling, but it's still confusing. Don't worry, we'll keep looking.
JScript Code

The code is as follows Copy Code

Alert (test instanceof String);
Alert (test1 instanceof String);

Based on the example above, we see that test is not instanceof string. You can also see that the typeof of Test is string in typeof. So we extend a question, what's the difference between a literal object and a constructor object? Answer: PROTOTYPE!!!!.
Well, we're solving the second problem, so we can push back the first question I just said, what new () did, in fact, new did something we needed to know. is to maintain the prototype chain.
Finally I make a hypothesis. Because I'm not sure that I'm right right now.
JScript Code

The code is as follows Copy Code
function func () {
}
var test = new Func ();
/* New in the process of doing the following behavior
var returnval = new Object ();
Returnval.prototype = this;//This step is not necessarily the case, we can understand the replication phase
If (ReturnVal is Object) {//This is instanceof more appropriate, or that the prototype chain can be found inherited from the object
return returnval;
} else
return obj;
*/

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.