test9.js

来自「《征服Ajax》原书的例题源码」· JavaScript 代码 · 共 49 行

JS
49
字号
Object.extend = function(destination, source) {
	for (property in source) {
		destination[property] = source[property];
	}
	return destination;}

//定义一个抽象基类base,无构造函数
function base() {
}

base.prototype = {
	initialize:function() {
		this.oninit();	//调用了一个虚方法
	}
}

function test() {
	// 构造函数
}

test.prototype = Object.extend( {
		prop : "prop",
		oninit : function() {
			alert(this.prop);
		}
	},
	base.prototype
)

function test2() {
	// 构造函数
}

test2.prototype = Object.extend( {
		prop2 : "prop2",
		oninit : function() {
			alert(this.prop2);
		}
	},
	base.prototype
)

var t = new test();
t.initialize(); // 输出"prop"

var t2 = new test2();
t2.initialize(); // 输出"prop2"

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?