枚举JavaScript对象的函数_javascript技巧_脚本之家

From:JavaEye.com 枚举JavaScript对象的函数: functioniterator{ for{
document.writeln(“

var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
概念了两个class函数作为成立类的模版大概说是原型
使用办法

“+property+”:”+obj[property]+”

复制代码 代码如下:

“卡塔尔; } } 贰个回顾示例: functionEmployee(State of Qatar{ this.name=””;
this.dept=”general”; } functionManager(卡塔尔国{ this.reports=[]; }
Manager.prototype=newEmployee(); functionWorkerBee(){
this.projects=[]; } WorkerBee.prototype=newEmployee();
functionSalesPerson(){ this.dept=”sales”; this.quota=100; }
SalesPerson.prototype=newWorkerBee{ this.dept=”engineering”;
this.machine=””; } Engineer.prototype=newWorkerBee();
Engineer.prototype.specialty=”code”; functioniterator{ for{
document.writeln(“

<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>

“+property+”:”+obj[property]+”

////
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页面为:

复制代码 代码如下:

发表评论

电子邮件地址不会被公开。 必填项已用*标注