<Title> Create your own object </title>
An error is reported.
How to Create constructors:
1. function is the starting point for creating constructors.
<SCRIPT type = "text/JavaScript"> function myconstructor (a) {/* some code */} </SCRIPT>
2. The above code is equivalent to the following code:
<SCRIPT type = "text/JavaScript"> var myconstructor = new function ('A', '/* some code */'); </SCRIPT>
3. The following new method is not recommended because of performance problems.
4. The special feature of the function object is that its instance can also be used as a constructor, so it can be used to create a new instance of the function. Therefore, you can write the following code:
<SCRIPT type = "text/JavaScript"> function myconstructor (a) {/* some code */} var myobject = new myconstructor (); </SCRIPT>
In this case, the myconstructor function can be used as the constructor. When an object is added to an instance, the constructor executes the contained code.
<SCRIPT type = "text/JavaScript"> var myobject = {}; // new object (); function myconstructor (Message) {/* some code */Alert (Message ); this. mymessage = message;} var myobj = new myconstructor ('helo World'); alert (""); </SCRIPT>
The result is as follows:
In this example: Remember 2 points:
1. This references the myobj instance. by assigning the message parameter to this. mymessage, myobj has an attribute named mymessage that can be accessed at any time. VaR message = myobj. mymessage;
2. The mymessage attribute is only available in the instance of the instantiated myconstructor, And the myconstructor function itself is invalid:
function myConstructor(message){this.myMessage="abc";}alert(myConstructor.myMessage);
If the task runs successfully, ABC is displayed. Right. But the result is:
This illustrates our conclusion.
Add static method:
<SCRIPT type = "text/JavaScript"> var myobject = new object (); // Add an attribute myobject. name = 'object'; // Add a method named myobject. alertname = function () {alert (this. name);} // execute the add method myobject. alertname (); </SCRIPT>
The token is printed.
In the above Code, we add the name attribute and alertname () method as static members to the object instance directly on the myobject object through the dot operator. This is the most likely cause of misunderstanding. Because the static members only exist in the specific instances of the object and do not exist in the constructor.
If you start with a function object, the same problem also exists, but this object instance is also a constructor.
var myConstructor=function(){}myConstructor.name='jh';myConstructor.alertName=function(){alert(this.name);}myConstructor.alertName();
The code can also run normally, because myconstructor is both an instance and a constructor, but the name and alertname members cannot be applied to any new instances. If you create an instance of myconstructor, the following code is returned:
<script type="text/javascript">var myConstructor=function(){}myConstructor.name='jh';myConstructor.alertName=function(){alert(this.name);}//myConstructor.alertName();var myObj=new myConstructor();myObj.alertName();</script>
An error occurs:
It is a static member. It only exists in a specific instance of the object and does not exist in the constructor.
For details, we usually say the two statements are the same:
var myConstructor=function(){ }function myConstructor(){}
However, there is a detailed problem. The first method must be defined before it can be called. If we call the method first, an error will be reported, but the second method does not.
TianAdd public method:
1. The methods contained in the instantiated object are called public methods. You need to modify the function prototype, that is, the prototype attribute.
2. The prototype attribute is used to define special members of the object's internal structure. If you modify prototype, the modification will be immediately applied to its derived objects and instances.
3. When you modify the prototype of an object, any object that inherits the object and all existing instances will immediately inherit the same changes. Depending on the usage, this feature is both powerful and may cause problems. Therefore, be careful when you modify the existing but not the prototype of your object.
<SCRIPT type = "text/JavaScript"> function myconstructor () {// some code this. mymessage = "ABC";} var myobj = new myconstructor (); alert (myobj. mymessage); myconstructor. prototype. myname = 'handler'; alert (myobj. myname); </SCRIPT>
This prints out ABC and signature.
To add a public method, you only need to use the dot operator to add a method to its prototype:
Add private and privileged members:
Private Members are the variables and functions defined in constructors.
For example, to add private methods and attributes to myconstructor, you only need:
Privileged method:
Unlike private methods, privileged methods can be publicly accessed and Private Members can be accessed. Privileged methods refer to the methods defined using the this keyword in the scope of constructors:
In the preceding method, myconstructor has the same appendtomessage () method as prototype. However, the method belongs to the constructor scope and can access private members.
Summary of public, private, privileged, and static:
1. Private and privileged members are inside the function, and they will be taken to every instance of the function, thus occupying a large amount of memory.
2. A public prototype member is part of an object's blueprint. It is applicable to instantiating each instance of the object using the new keyword.
3. Static members are only applicable to one special instance of the object.