js constructor return

This link: https: //blog.csdn.net/qq_36209248/article/details/89190978


By default, there is no return of the function return value is undefined (i.e., does not define a return value), if the return is defined, the specified object is returned.
But the constructors compare t special, new default constructor returns the newly created object in the case of no return. In the case of return of the case, it is divided into two cases to consider:

If the return value is the basic data type (string, number, boolean, undefined , null), then the new object instance return value, i.e. this.
A function S = var () {
  in this.x =. 3;
  return. 1;
}
var B = new new A ();
the console.log (A); // {X:. 3}

If the return value is a non-basic data types the object, the function returns a value of the specified object, the object referenced by this value is discarded.
A function S = var () {
  in this.x =. 3;
  return A;
}
var B = new new A ();
the console.log (B);} A //S(){this.x=3;return

intuitive example:

var a = function User( name, age){
  this.name = name;
  this.age = age;

  // return; // return the this
  // return null; // return the this
  // return the this; // return the this
  // return to true; // return the this
  // return 'String'; // return the this
  // return. 1 ; // return the this
  // returns above {name: "ha", Age: 18 is}
  // return []; // return new []
  // return function () {}; // return the new function, discard the this
  // return new new Boolean (to false); // returns the new boolean, discard the this
  // return new new String ( 'Hello World'); // returns the new string, discard the this
  // Number the new new return (32); // returns the new number, abandon the this
}
var b = new new a ( "ha ha", 18)
console.log (b);

mainly JS - new and return the contents of their own to do a bit of finishing, for their future review.
----------------
Disclaimer: This article is CSDN blogger "still missing boyfriend" original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_36209248/article/details/89190978

Guess you like

Origin www.cnblogs.com/jeff-zhu/p/11441599.html