prototype 学习笔记整理

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

复制代码 代码如下:


<html>
<title>Test Class.create()</title>
<head>
<script language="JavaScript" type="text/javascript" src="https://www.jb51.net/prototype.js"></script>
<script>
var llinzzi= Class.create();
llinzzi.prototype = {
initialize:function(){
document.writeln('This is create when initialize');
},
fuv:function(){document.writeln('This is inline method');}
}
var linChild = new llinzzi();
</script>
</head>
<body>
<script>
//window.onload(linChild);
window.onload(linChild.fuv());
</script>;
</body>
</html>


////
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);
使用 例子:

复制代码 代码如下:


<html>
<title>Test Object.extend</title>
<head>
<script language="JavaScript" type="text/javascript" src="https://www.jb51.net/prototype.js"></script>
<script>
function log(message) {
document.writeln(" >>>>>: " +message);
}
var obj1= {
method : "post",
args : ""
};
var obj2 = Object.extend({}, obj1);
log(obj2.method);
log(obj1 == obj2);
log(obj1.method);
log(obj2 == obj1);
</script>
</head>
<body>
</body>
</html>


// 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!"); }
使用例子:

复制代码 代码如下:


<html>
<title>Test Object.extend</title>
<head>
<script language="JavaScript" type="text/javascript" src="https://www.jb51.net/prototype.js"></script>
<script>
function log(message) {
document.writeln(" >>>>>: " +message);
}
var options= {
method : "post",
args : ""
};
Object.extend(options, {
args : "data=454",
onComplete : function() { alert("done!");}
});
options.method // "post"
options.args // "ata=454"
options.onComplete // function() { alert("done!"); }
log(options.method);
log(options.args);
log(options.onComplete);
</script>
</head>
<body>
</body>
</html>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwwsp.html