not practical new, that is, ordinary function calls, so if the function itself does not return a value, ordinary function calls have no meaningsuch as: Var person=new person ();//person is an object
The var person = person ()//This is just an ordinary function call and assign a value.
Example one:
function Person (name,age) {
this.name=name;
This.age=age;
This.sayname=function () {
alert (this.name);
};}
var person=new person ("John", 20); The object is constructed here, and the object is constructed, and the new object returned is generated by the parser itself.
var Person=person ("John", 20);//Suppose I add the return "Hello" to the person function, and the person will not report the undefined, but a string. Hello
Person.sayname ()//error person undefined here for normal function calls, without a given return value, errors.
//Because this point to the Window object,
window.sayname ()//At this point is not an error
then asked, why I assign value to person, you can use window to reference it.
because if no new is equivalent to a normal function call, and the person () does not return a value at all,
so the person is not assigned to person, the person at this time is only a undefined,
But the person executes once, becomes the object of window, this points to window, so window can use person's method directly, person
("John",);
Person.sayname ();
Example two: if the function returns a value of a numeric type (number, String, Boolean) in the normal sense, the new function will return an instance object of that function, and if the function returns a reference type (object, Array, Functions), the new function is the same as the result of the direct call function. are as follows:
function test ()
{
this.name = ' Test ';
return "Test";
}
var test1 = new Test (); Object, which has a Name property and returns a string of test
var test2 = Test (); function test () belongs to a function object this test2, which is simply a string
Factory mode:
<script type= "Text/javascript" >
/*//Factory mode
function person (name,age)
{
var o = new Object ();
O.name = name;
O.age = age;
O.getname = function ()
{
alert (this.name);
}
return o;
}
var obj1 = new Person ("Liwen");
var obj2 = person ("Liwen1");
Obj1.getname (); liwen*/ new Instance object of a function
obj2.getname (); liwen1*/ Direct Call
here new The object of a function is the same as the result of a direct call function, which can pop up the name property of the function. Note that the return value of the function here is a Funtion object
Process: constructor does not need to display the return value. When you use new to create an object (call a constructor), if the return is a non object (number, String, Boolean type, and so on), it returns a value, and returns the object if it is an object. the Following is a brief introduction to the process of the new object in javascript: such as var myobj = Newperson ("Aty"); 1. Create an empty object object. var obj = new Object (); 2. In the constructor person, this points to the Obj object you just created 3. The __proto__ of obj to be created points to the prototype of the constructor person. This step is to establish a direct correspondence between objects and prototypes. Firefox can access the prototype through the object's __proto__ property, ie does not expose the corresponding attributes. 4. Execute the code in the constructor person ()