SoFunction
Updated on 2025-02-28

javascript Prototype object extension

Javascript is of course no exception, but have you considered the issue of object reference? The usual practice is to share a series of methods of objects sharing classes, rather than copying a copy of a function for each object. Let’s take a look at the practice of copying a function for each object.
Copy the codeThe code is as follows:

var myobject=function(param1,param2)
{
=param1;
=param2;
=function()
{
alert("name:"++"\n"+"age:"+);
}
}
var objectone=new myobject('liu',20);
alert(); //liu
var objecttwo=new myobject('lin',20);
alert(); //lin
alert(());
var objectthree=new myobject('lhking',22);
alert(());

It looks good, and it is also good to use. The objects do not interfere with each other and can complete the work normally. Everything seems to be natural, but every time you generate a new object, the script engine will copy a copy of the attributes and methods to the object. Do you think this is a waste of memory? This way of copying objects on the client is extremely easy to cause memory leakage, because every time a new instance is generated, all attributes and methods will be copied, occupying a lot of memory.
The first thing to consider in large JavaScript applications is memory issues.
The correct way to use it is to use the prototype keyword to define the method or attribute of a class.
Copy the codeThe code is as follows:

var myobject=function(param1,param2)
{
=param1;
=param2;
=function()
{
alert("name:"++"\n"+"age:"+);
}
}
var objectone=new myobject('liu',20);
alert();
var objecttwo=new myobject('lin',20);
alert();
alert(());
var objectthree=new myobject('lhking',22);
alert();

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

Take a look at object extensions in JavaScript
Copy the codeThe code is as follows:

function rand(x)
{
return (()*x);
}
An extension method for generating random arrays
=function()
{
for(var i=(-1);i>1;i--)
{
var j=rand(-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]);
}
();
for(var b in array)
{
alert(array[b]);
}

Look at object reflections
Reflection is an object mechanism, which allows you to understand its properties and methods without understanding the object at all. Usually, programmers are very familiar with how the objects they manipulate are composed. However, 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, and we need to use the reflection mechanism. Of course, the application of reflection is not limited to this. Here we only introduce the use of reflection in Javascript.

First of all, we may want to know whether there is a specific property or method in an object, and then we can test it:
if(typeof()!="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 scope of the test:
if(myobject instanceof Object)
{
}
instanceof is an operator used to test built-in classes or custom classes. Built-in classes refer to built-in classes such as Array, String, Date, Number, Math, RegExp, Boolean, Function, etc. For example: Function and Array are inherited from the Object class, so if you test an Array object in your code, if you first test whether it is an Object, it will return true, and if you test whether it is an Array, it will also return true.

A simpler and more useful method is to traverse all properties and methods of an object to quickly understand the internal state of an object:
Copy the codeThe code is as follows:

function myobject(){
="name";
="age";
="sex";
=function(){
}
}
var myobj=new myobject();
for(var i in myobj){
alert(myobj[i]);
}