基于JavaScript 类的使用详解
2013-05-07 00:00
627 查看
以下为构造函数方法创建类:
有了上面的类,我们就可以为类创建实例:
我们也可以给类添加方法(method),其实就是Function里的Function。
属性访问域:
在JavaScript里,对象的属性默认都是全局的,也就是说,对象内外都可以直接访问该属性。上面例子里this.prop1, this.prop2, this.prop3都是全局属性。
如何定义私有属性呢?使用var,下面的例子里,price就变成了私有属性!
如果你想访问私有属性,那么你可以在对象内添加一个方法去返回这个私有属性,因为方法在对象内,所以可以访问对象的私有属性。在外部调用该方法,就可以访问到这个私有属性了。但是在方法里,就不能再用this.了,像上面的例子,要访问price,就可以在对象里添加方法:
继承:
使用以下语法继承:
使用instanceOf检查对象是否某对象的继承,返回true或false。
给继承后的对象添加方法:
重写原型对象的值或方法:
当我们继承原型对象后,我们会继承原型的值和方法。但有的时候,我们的对象值或方法可能会不同,这时候,我们可以通过重写原型对象的值和方法来改变新对象的内容
function className (prop_1, prop_2, prop_3) { this.prop1 = prop_1; this.prop2 = prop_2; this.prop3 = prop_3;}
有了上面的类,我们就可以为类创建实例:
var obj_1 = new className(v1, v2, v3) var obj_2 = new className(v1, v2, v3)
我们也可以给类添加方法(method),其实就是Function里的Function。
function className (prop_1, prop_2, prop_3) { this.prop1 = prop_1; this.prop2 = prop_2; this.prop3 = prop_3; this.func = function new_meth (property) { //coding here } }
属性访问域:
在JavaScript里,对象的属性默认都是全局的,也就是说,对象内外都可以直接访问该属性。上面例子里this.prop1, this.prop2, this.prop3都是全局属性。
如何定义私有属性呢?使用var,下面的例子里,price就变成了私有属性!
function Car( listedPrice, color ) { var price = listedPrice; this.color = color; this.honk = function() { console.log("BEEP BEEP!!"); }; }
如果你想访问私有属性,那么你可以在对象内添加一个方法去返回这个私有属性,因为方法在对象内,所以可以访问对象的私有属性。在外部调用该方法,就可以访问到这个私有属性了。但是在方法里,就不能再用this.了,像上面的例子,要访问price,就可以在对象里添加方法:
this.getPrice = function() { //return price here! return price; --------------------------------------------------------------------------------
继承:
使用以下语法继承:
ElectricCar.prototype = new Car();
使用instanceOf检查对象是否某对象的继承,返回true或false。
myElectricCar instanceof Car
给继承后的对象添加方法:
// 使用构造函数定义一个新的对象
function ElectricCar( listedPrice ) {
this.electricity=100;
var price = listedPrice;
}
// 使新对象继承CarElectricCar.prototype = new Car();
// 为新对象添加方法
ElectricCar.prototype.refuel = function(numHours) {
this.electricity =+ 5*numHours;
};
重写原型对象的值或方法:
当我们继承原型对象后,我们会继承原型的值和方法。但有的时候,我们的对象值或方法可能会不同,这时候,我们可以通过重写原型对象的值和方法来改变新对象的内容
function Car( listedPrice ) {
var price = listedPrice;
this.speed = 0;
this.numWheels = 4;
this.getPrice = function() {
return price;
};
}
Car.prototype.accelerate = function() {
this.speed += 10;
};
function ElectricCar( listedPrice ) {
var price = listedPrice;
this.electricity = 100;
}ElectricCar.prototype = new Car();
// 重写accelerate方法
ElectricCar.prototype.accelerate = function() {
this.speed += 20;
};
// 添加新方法decelerateElectricCar.prototype.decelerate = function(secondsStepped) {
this.speed -= 5*secondsStepped;
};
myElectricCar = new ElectricCar(500);
myElectricCar.accelerate();
console.log("myElectricCar has speed " + myElectricCar.speed);
myElectricCar.decelerate(3);
console.log("myElectricCar has speed " + myElectricCar.speed);
相关文章推荐
- 基于JavaScript 类的使用详解
- 基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
- 基于JavaScript实现继承机制之构造函数方法对象冒充的使用详解
- 基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
- 基于JavaScript实现继承机制之构造函数+原型链混合方式的使用详解
- 基于Android中Webview使用自定义的javascript进行回调的问题详解
- JavaScript中var关键字的使用详解
- 基于PHP选项与信息函数的使用详解
- [ASP.NET]基于ime-mode属性使用详解
- 详解JavaScript的while循环的使用
- Javascript 使用ajax与C#获取文件大小实例详解
- 基于iOS Realm数据库的使用实例详解
- Javascript的调试利器:Firebug使用详解1(转)
- 基于vue中css预加载使用sass的配置方式详解
- 基于MySQL游标的具体使用详解
- 详解JavaScript中void语句的使用
- 详解JavaScript中shift()方法的使用
- JavaScript打开窗口函数(Window.Open)使用详解、绝对路径与相对路径的获取与转换(含ASP.NET)
- javascript中的正则表达式使用详解
- JavaScript中的Math.sin()方法使用详解