JScript的继承可以通过两种方式来实现,一种是通过对象的prototype属性,一种是通过call函数。第一种方式同时也适用于Javascript,第二种方式只适用于JScript。
下面的例子是用prototype实现的继承:
var Person = function() {
this.name = "Person";
}
this.name = "Person";
}
var _p = Person.prototype;
_p.getName = function() {
return this.name;
}
_p.setName = function(name) {
this.name = name;
}
return this.name;
}
_p.setName = function(name) {
this.name = name;
}
var Jeffrey = function() {
this.name = "Jeffrey";
this.name = "Jeffrey";
this.say = function(word) {
alert(word);
}
}
alert(word);
}
}
_p = Jeffrey.prototype = new Person();//实现继承
_p.say = function(word) {
alert(word);
}
_p.say = function(word) {
alert(word);
}
通过下面的代码进行测试:
var jeffrey = new Jeffrey();
alert(jeffrey.getName());//调用父对象定义的方法
jeffrey.setName("Jeffrey He");//调用父对象定义的方法
alert(jeffrey.getName());
jeffrey.say("Hello");
再看看用call函数实现的例子:
function Person() {
this.name = "Person";
this.getName = function() {
return this.name;
}
this.setName = function(name) {
this.name = name;
}
}
function Jeffrey() {
Person.call(this);//实现继承
this.name = "Jeffrey";
this.say = function(word) {
alert(word);
}
}
测试代码同上。