Nowadays, many people use js objects to implement code after learning jsp, but in JavaScript, js objects are a dynamic language. Many friends who are just getting started will be curious about what are the object-oriented writing methods of JavaScript? What's the difference? Let me now explain the JavaScript object-oriented writing methods and differences.
In JS, there are generally two ways to implement OOP:
The first: Use this keyword
function Class1()
{
this.onclick = function(e)
{
for (var i=0; i < 1000; i++)
{
var a = new Date();
}
}
}
Using this. method can add properties and methods to objects flexibly, and it is similar to most OOP languages, and can even be added during operation.
The second type: use prototype keywords
function clickFunc(e)
{
for (var i=0; i < 1000; i++)
{
var a = new Date();
}
}
function Class2()
{
}
Class2.prototype.onclick = clickFunc;
In terms of usage, there is no first one that seems flexible. However, before an object is new, you can also add the properties and methods of an object at any time.
But they are not equal. Relatively speaking, I prefer the first one because the first method is relatively concentrated and easier to read the code. However, when running, their operating efficiency varies greatly. Let's take a look at the test code below:
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 Test1() takes 142ms, while Test2() only takes 50ms. In terms of time efficiency, the prototype method is more efficient than this.
The second step of javascript objects is to test memory usage, remove the comments on total.push(c); in total.push(c); so that they are added to the array to prevent the unreferenced objects from being GC when there are many objects when they are created. As a result, it was found that the gap was not very large. The first method took up twenty or thirty M of memory, while the second method only required more than one hundred K.
javascript create object
Cause inference:
When processing these two codes, the first type is JS parser, which creates a separate method for each object, which increases memory overhead and increases running time when creating methods. The second type is that JS parser, like most OOP compilers, stores the object's data segments and method segments separately. For the object's private data, one copy per object, and these methods are placed in the public method segment, so the running time and memory overhead can be reduced.
The above is the editor of Fo Xin to explain the differences in object-oriented writing methods and differences of JavaScript. If you want to know more deeply, you can go to Fo Xin Technology Channel to understand.