Home  >  Article  >  Web Front-end  >  prototype study notes organization_prototype

prototype study notes organization_prototype

WBOY
WBOYOriginal
2016-05-16 18:49:471022browse

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
A class function is defined as a template or prototype for creating a class
Usage method

Copy code The code is as follows:


Test Class.create()


<script> <br>var llinzzi= Class.create(); <br>llinzzi.prototype = { <br>initialize: function(){ <br>document.writeln('This is create when initialize'); <br>}, <br>fuv:function(){document.writeln('This is inline method');} <br> } <br>var linChild = new llinzzi(); <br></script>


<script> <br>//window.onload (linChild); <br>window.onload(linChild.fuv()); <br></script>;



////
This is create when initialize This is inline method;
/////
That is, when the prototype's Class.create(); method is used to create an object, initialize is used as Special methods are executed when creating an instance for initialization.
Inherited
Object.extend = function(destination, source) {
for (var property in source) {
destination[ property] = source[property];
}
return destination;
}
This method will copy all the properties and methods of the source object to the destination object.
Prototype is performed on the Object class Extension mainly implements inheritance in JavaScript through a static function Object.extend (destination, source). From a semantic point of view, the Object.extend (destination, source) method is somewhat illogical, because it actually only implements a holographic copy from the source object to the destination object. But you can also think of it this way: since the target object has all the characteristics of the source object, it looks like the target object inherits the source object (and extends it).
// make a (shallow) copy of obj1
var obj1 = {
method : "post",
args : ""
};
var obj2 = Object.extend({}, obj1);
Usage example :
Copy code The code is as follows:


Test Object.extend


<script> <br>function log(message) { <br>document.writeln(" >>>>>: " message); <br>} <br>var obj1= { <br> method : "post", <br>args : "" <br>}; <br>var obj2 = Object.extend({}, obj1); <br>log(obj2.method); <br>log(obj1 == obj2); <br>log(obj1.method); <br>log(obj2 == obj1); <br></script>





// merges in the given options object to the default options object
Object.extend(options, {
args : "data=454",
onComplete : function() { alert("done!"); }
});
options.method // "post"
options.args / / "ata=454"
options.onComplete // function() { alert("done!"); }
Usage example:
Copy code The code is as follows:


Test Object.extend


<script> <br>function log(message) { <br>document. writeln(" >>>>>: " message); <br>} <br>var options= { <br>method : "post", <br>args : "" <br>}; <br>Object.extend(options, { <br>args : "data=454", <br>onComplete : function() { alert("done!");} <br>}); <br>options. method // "post" <br>options.args // "ata=454" <br>options.onComplete // function() { alert("done!"); } <br>log(options.method); <br>log(options.args); <br>log(options.onComplete); <br></script>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn