首页  >  文章  >  web前端  >  prototype 学习笔记整理_prototype

prototype 学习笔记整理_prototype

WBOY
WBOY原创
2016-05-16 18:49:471073浏览

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
定义了一个class函数作为创建类的模版或者说是原型
使用方法

复制代码 代码如下:


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 ;
/////
就是当采用了prototype的Class.create();方法创建对象的时候,initialize作为特殊的方法,在创建实例的时候被执行,用以初始化.
继承
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
此方法将拷贝所有的source object的属性和方法到destination object.
Prototype 对Object类进行的扩展主要通过一个静态函数Object.extend (destination, source)实现了JavaScript 中的继承。 从语义的角度, Object.extend (destination, source)方法有些不和逻辑, 因为它事实上仅仅实现了从源对象到目标对象的全息拷贝。不过你也可以这样认为:由于目标对象拥有了所有源对象所拥有的特性, 所以看上去就像目标对象继承了源对象(并加以扩展)一样.
// make a (shallow) copy of obj1
var obj1 = {
method : "post",
args : ""
};
var obj2 = Object.extend({}, obj1);
使用 例子:
复制代码 代码如下:


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!"); }
使用例子:
复制代码 代码如下:


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>




声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn