JavaScript-oriented object-oriented methods and differential _js object-oriented

Source: Internet
Author: User
First: Use the This keyword

function Class1 ()
{
This.onclick = function (e)
{
for (Var i=0 i < 1000; i++)
{
var a = new Date ();
}
}

}

Using this. method provides the flexibility to add properties and methods to an object, and is similar to most OOP languages and can be added even in run.

Second: Using the prototype keyword

function Clickfunc (e)
{
for (Var i=0 i < 1000; i++)
{
var a = new Date ();
}
}

function Class2 ()
{

}

Class2.prototype.onclick = Clickfunc;

There is no first kind of flexibility in this usage. But before an object is new, you can add an object's properties and methods at any time.

But they are not equal, relatively speaking, I prefer the first, because the first method is relatively concentrated, easier to read the code. But when they run, the efficiency of their operations varies considerably. Let's take a look at the test code here:

var total = new Array ();

function Test1 ()
{
var a = new Date ();
for (Var i=0 i < 10000; i++)
{
var c = new Class1 ();
Total.push (c);
}
var B = new Date ();
Alert (B.gettime ()-a.gettime ());
}

function Test2 ()
{
var a = new Date ();
for (Var i=0 i < 10000; i++)
{
var c = new Class2 ();
Total.push (c);
}
var B = new Date ();
Alert (B.gettime ()-a.gettime ());
}

The first step is to test the execution time: it is found that the Test1 () requires 142ms, while the TEST2 () only needs 50ms. The prototype method is more efficient than this one for discovering time efficiency.

The second step for memory footprint test, the Total.push (c), this line of comments removed, the reason to add them to the array to prevent the creation of the time, the object is more, the object is not referenced by GC. The results found that the gap is not generally large, the first method to occupy twenty or thirty m of memory, and the second only need more than 100 K.

Genesis Inference:
When dealing with both types of code, the first, JS parser, creates a separate method for each object, which increases the overhead of the memory and increases the elapsed time when creating methods. The second type of JS parser is the same as most OOP compilers, the object's data segment and method segment are stored separately, for the object's private data is a copy of each object, and these methods are placed in the public method segment, so you can reduce the running time and memory overhead.

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.