JavaScript Prototype object Extension _javascript Tips

Source: Internet
Author: User
Tags rand reflection
JavaScript, of course, is no exception, but about the object of reference issues, you have considered it? The common practice is to have a series of methods for sharing classes, rather than duplicating a function for each object. Here's a look at copying a function for each object.
Copy Code code 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 ());

Looks good, uses is also good, the object does not have any interference with each other, also can complete the work normally, all looks certainly, but every time you produce a new object, the script engine will give the object to copy a attribute and the method, does it feel that this is wasting memory? The way this object is replicated at the client is extremely vulnerable to memory leaks, because every time a new instance is generated it replicates all the properties and methods and consumes a lot of memory.
The first thing to consider in a large JavaScript application is the memory problem.
The correct way to do this is to use the prototype keyword to define a class's method or attribute
Copy Code code 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 case, you can create a common method between objects, that is, the showmsg () function is defined only once, and other objects share this method, rather than copying their own methods.
The prototype in JavaScript has been finished.

Look at the object extensions in JavaScript
Copy Code code 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]);
}

Then look at the object reflection
Reflection is the mechanism of an object, it allows you to understand its properties and methods without having a complete understanding of the object, and typically, programmers are very knowledgeable about how the objects they manipulate are composed, but in some special cases, when using a complex object written by someone else, We need to quickly understand the properties and methods of this object, we need to use the mechanism of reflection, of course, the application of reflection is not limited to this, here is only to introduce the use of reflection in JavaScript.

First we might want to know whether a particular attribute or method exists in an object, and then we can test it:
if (typeof (Myobject.someproperty)!= "undefined")
{
}
In JavaScript, if you don't define an object or variable, it always returns the undefined type.
You can also use other built-in classes to narrow your test scope:
if (MyObject instanceof Object)
{
}
Instanceof is used to test the operator of the built-in class or the custom class, and the built-in class refers to the built-in classes such as array,string,date,number,math,regexp,boolean,function. For example: The function and array are inherited from the object class, so if you test an array object in your code, if you test it for object first, it returns true, and it returns true if you test whether it is an array.

A simpler and more useful approach is to iterate through all the properties and methods of an object to quickly understand the internal state of an object:
Copy Code code 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.