下面本来是讲构造函数、原型继承的,可我看了老半天,很迷糊。本来以为已经理解这些东西了,可老是觉得不对劲。后来,刹那间明白,bus 、Bus 不同啊! Javascript is Case Sensitivity! I couldn't case of it!
var Bus;
//构造函数
if (Bus == undefined) {
Bus = function(name, num) {
this.Init(name, num);
};
}
Bus.prototype.Init = function(name, num) {
this.Name = name;
this.Num = num;
}
//静态成员
Bus.City = "静态北京";
Bus.GetCity = function() {
return Bus.City;
} //实例成员
Bus.prototype.City = "实例北京";
Bus.prototype.Show = function() {
return this.Name + "-" + this.Num;
} var bus = new Bus("公交车", "100");
alert(bus.City); //实例北京
alert(bus.Show()); //公交车 - 100
alert(Bus.City); //静态北京
alert(Bus.GetCity()); //静态北京
var Bus;
//构造函数
if (Bus == undefined) {
Bus = function(name, num) {
this.Init(name, num);
};
}
Bus.prototype.Init = function(name, num) {
this.Name = name;
this.Num = num;
}
//静态成员
Bus.City = "静态北京";
Bus.GetCity = function() {
return Bus.City;
} //实例成员
Bus.prototype.City = "实例北京";
Bus.prototype.Show = function() {
return this.Name + "-" + this.Num;
} var bus = new Bus("公交车", "100");
alert(bus.City); //实例北京
alert(bus.Show()); //公交车 - 100
alert(Bus.City); //静态北京
alert(Bus.GetCity()); //静态北京
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货