Javascript Prototype object extension

Source: Internet
Author: User

Javascript is no exception, but have you considered object reference? The common practice is a series of object sharing class methods, instead of copying a function for each object. The following describes how to copy a function for each object.
Copy codeThe Code is as follows:
Var myobject = function (param1, param2)
{
This. name = param1;
This. age = param2;
This. showmsg = function ()
{
Alert ("name:" + this. name + "\ n" + "age:" + this. age );
}
}
Var objectone = new myobject ('Liu ', 20 );
Alert (objectone. name); // liu
Var objecttwo = new myobject ('lin', 20 );
Alert (objecttwo. name); // lin
Alert (objecttwo. showmsg ());
Var objectthree = new myobject ('lhking', 22 );
Alert (objectthree. showmsg ());

It looks good, and it looks good to use. objects do not interfere with each other, and work can also be done normally. Everything seems to be taken for granted, but every time you create a new object, the script engine will copy attributes and methods for objects. Do you think this is a waste of memory? This method of copying objects on the client is extremely prone to memory leakage, because every time a new instance is generated, all attributes and methods are copied, occupying a large amount of memory.
Memory is the first consideration for large JavaScript applications.
The correct method is to use the prototype keyword to define the methods or attributes of a class.
Copy codeThe Code is as follows:
Var myobject = function (param1, param2)
{
This. name = param1;
This. age = param2;
Myobject. prototype. showmsg = function ()
{
Alert ("name:" + this. name + "\ n" + "age:" + this. age );
}
}
Var objectone = new myobject ('Liu ', 20 );
Alert (objectone. name );
Var objecttwo = new myobject ('lin', 20 );
Alert (objecttwo. name );
Alert (objecttwo. showmsg ());
Var objectthree = new myobject ('lhking', 22 );
Alert (objectthree. showmsg );

In this way, you can share methods between created objects, that is, the showmsg () function is defined only once. Other objects share this method instead of copying their own methods.
The prototype in Javascript has been completed.

Look at object extensions in JavaScript
Copy codeThe Code is as follows:
Function rand (x)
{
Return Math. ceil (Math. random () * x );
}
An extension method for Generating Random Arrays
Array. prototype. random = function ()
{
For (var I = (this. length-1); I> 1; I --)
{
Var j = rand (this. length-1 );
Var cache = this [I];
This [I] = this [j];
This [j] = cache;
}
}
Var array = new Array ("1", "2", "3", "4", "5 ");
For (var a in array)
{
Alert (array [a]);
}
Array. random ();
For (var B in array)
{
Alert (array [B]);
}

Let's look at object reflection.
Reflection is an object mechanism that allows you to understand its attributes and methods without understanding the object at all. Generally, programmers are very familiar with how the objects they manipulate are made up, but in some special circumstances, when using a complex object written by someone else, we need to quickly understand the attributes and methods of this object, so we need to use the reflection mechanism. Of course, the reflection application is not limited to this. Here we only introduce the use of reflection in Javascript.

First, we may want to know whether a specific attribute or method exists in an object. At this time, we can test it:
If (typeof (myobject. someproperty )! = "Undefined ")
{
}
If no object or variable is defined in Javascript, it always returns the undefined type.
You can also use other built-in classes to narrow the test scope:
If (myobject instanceof Object)
{
}
Instanceof is an operator used to test built-in or custom classes. built-in classes refer to built-in classes such as Array, String, Date, Number, Math, RegExp, Boolean, and Function. for example, both Function and Array inherit from the Object class. Therefore, if you test an Array Object in the code, true is returned if you first test whether it is an Object, if you test whether it is an Array, it returns true.

A simpler and useful method is to traverse all attributes and methods of an object to quickly understand the internal state of an object:
Copy codeThe Code is as follows:
Function myobject (){
This. name = "name ";
This. age = "age ";
This. sex = "sex ";
This. func = function (){
}
}
Var myobj = new myobject ();
For (var I in myobj ){
Alert (myobj [I]);
}

Related Article

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.