JavaScript Advanced Knowledge Analysis--instantiation

Source: Internet
Author: User

The code information comes from http://ejohn.org/apps/learn/.

What has new been doing?
function Ninja () {   this. Name = "Ninja";}    var ninjaa ="Undefined,ninja is not instantiated" );   var New  = = "Ninja", "true, existence of name attribute in instantiation")

The new operation of a function is instantiated.

The process of the above code is to create a temporary object, inherit the Ninja.prototype object, execute the function ninja, and initially set the context this to a temporary object. Returns the temporary object without a return statement.

Our context this points to the instantiated object
functionNinja () { This. swung =false; //should return True   This. Swingsword =function(){      This. swung =! This. Swung; return  This. Swung; }; }  varNinja =NewNinja (); Console.log (Ninja.swingsword (),"Invoke Instance Object method" ); Console.log (Ninja.swung,"Referencing instance object Properties" ); varNinjab =NewNinja (); Console.log (!ninjab.swung, "determines that the property referenced by this is a property of the instantiated object, and that the instantiated object does not affect each other.");
Exercise: Add a method that can give ninja a Name property
function Ninja (name) {   // complement }  varnew Ninja ("John"= = "John", "Name property is set at initialization" );  Ninja.changename ("Bob"= = "Bob", "Name Value modified successfully");
Add a new property and method to the instantiated object
function Ninja (name) {   thisfunction(name) {     this. Name = name ;  }   this. ChangeName (name)}  varnew Ninja ("John"= = " John "," The name attribute is set when initializing " );  Ninja.changename ("Bob"= = "Bob", "Name Value modified successfully");
What happens when new is not used?
function User (First, last) {   this. Name = First + "" + Last ;}    var user = User ("John", "Resig"typeof user = = "undefined", "because there is no new, so the user makes a general function call, no return value");
function User (First, last) {   this. Name = First + "" + Last ;}    = "Resig"var user = User ("John", name);   = = "John resig", "Global attribute is overwritten");

When there is no new, it is actually a general function call, followed by the nature of the function call.

Ensure that object instantiation is still used in case of error
function User (First, last) {   if (!) (  This instanceof User))      return New User (First, last);    this. Name = First + "" + Last ;}   var name = "Resig"var user = User ("John", name);  " Even if you do not use new, you can stillproperly instantiate "= =" Resig "," instantiation of the property is normal ");

By judging this is not an instantiated object of the constructor user, it is re-instantiated, a technique that can be instantiated without using new.

Exercise: Is there a more genetically-structured way to do the same thing?
function User (First, last) {   if (!) (  This instanceof  ___) )      return New User (First, last);    this. Name = First + "" + Last ;}   var name = "Resig"var user = User ("John", name);  " Even if you do not use new, you can stillproperly instantiate "= =" Resig "," instantiation of the property is normal ");
Implemented with Arguments.callee
function User (First, last) {   if (!) (  This instanceof  ___) )      return New User (First, last);    this. Name = First + "" + Last ;}   var name = "Resig"var user = User ("John", name);  " Even if you do not use new, you can stillproperly instantiate "= =" Resig "," instantiation of the property is normal ");

Arguments.callee refers to the function itself.

JavaScript Advanced Knowledge Analysis--instantiation

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.