一.运用prototype属性
定义对象 A:
function A() { ... }
给A添加属性或方法
A.prototype.aa
A.prototype.bb = function() { ... }
创建一个对象继承 A 对象
function B() {}
B.prototype = new A();
重写 A 对象的 bb 方法
B.prototype.bb = function() { ... }
二.对象属性的复制
function createInheritance( parent, child ) {
var property;
for( property in parent ) {
if( !child[ property ] ){
child[ property ] = parent[ property ];
}
}
}
注:私有属性
用 var 在构造函数中定义的变量或在其内部定义的 function.
可以用 this 关键词定义特权函数 ( privileged function ) 来访问.
function A()
{
var aa;
function bb() { ... }
this.cc() { bb(); }
}
定义对象 A:
function A() { ... }
给A添加属性或方法
A.prototype.aa
A.prototype.bb = function() { ... }
创建一个对象继承 A 对象
function B() {}
B.prototype = new A();
重写 A 对象的 bb 方法
B.prototype.bb = function() { ... }
二.对象属性的复制
function createInheritance( parent, child ) {
var property;
for( property in parent ) {
if( !child[ property ] ){
child[ property ] = parent[ property ];
}
}
}
注:私有属性
用 var 在构造函数中定义的变量或在其内部定义的 function.
可以用 this 关键词定义特权函数 ( privileged function ) 来访问.
function A()
{
var aa;
function bb() { ... }
this.cc() { bb(); }
}