当前位置: 首页 > 图文教程 > 网络编程 > Javascript > prototype 学习笔记整理

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 prototype 学习笔记整理


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 356 ::
收藏到网摘: n/a

prototype 学习笔记整理,学习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="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>