prototype 學習筆記整理_prototype

來源:互聯網
上載者:User
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="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="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="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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.