面向对象的JS(2) JavaScript对面向对象的支持

1 实例方法
实例方法是由实例对象访问的方法,实例对象由new操作符创建。实例成员通过this关键字、prototype、构造器闭包或者Object.defineProperty来创建

function Cat(name) {
var voice = "Meow";
this.name = name;
this.say = function () {
return voice;
}
}
Cat.prototype.eat = function () {
return "Eating";
}
var cat = new Cat("Fluffy");
Object.defineProperty(cat, "numLegs", {
value: 4,
writable: true,
enumerable: true,
configurable: tr
ue
});
console.log(cat.name); // Fluffy
console.log(cat.numLegs); // 4
console.log(cat.say()); // Meow
console.log(cat.eat()); // Eating

2 静态方法
JavaScript中没有对静态成员的直接支持。构造函数可以用来创建静态成员,静态成员不能被"this"关键字访问。
公有静态成员例子:

function Factory() {}
// public static method
Factory.getType = function () {
return "Object Factory";
};
// public static field
Factory.versionId = "F2.0";
Factory.prototype.test = function () {
console.log(this.versionId); // undefined
console.log(Factory.versionId); // F2.0
console.log(Factory.getType()); // Object Factory
}
var factory = new Factory();
factory.test();

私有静态成员例子:

var Book = (function () {
// private static field
var numOfBooks = 0;
// private static method
function checkIsbn(isbn) {
if (isbn.length != 10 && isbn.length != 13)
throw new Error("isbn is not valid!");
}

function Book(isbn, title) {
checkIsbn(isbn);
this.isbn = isbn;
this.title = title;
numOfBooks++;
this.getNumOfBooks = function () {
return numOfBooks;
}
}
return Book;
})();
var firstBook = new Book("0-943396-04-2", "First Title");
console.log(firstBook.title); // First Title
console.log(firstBook.getNumOfBooks()); // 1
var secondBook = new Book("0-85131-041-9", "Second Title");
console.log(firstBook.title); // First Title
console.log(secondBook.title); // Second Title
console.log(firstBook.getNumOfBooks()); // 2
console.log(secondBook.getNumOfBooks()); // 2

3 抽象类型:
JavaScript是一种弱类型的语言,所以定义变量时不需要指定具体的类型。这减少了像接口这样的抽象层次需要。但当我们需要继承的时候,我们可以将一些公共函数放入抽象类中。

(function () {
var abstractCreateLock = false;
// abstract type
function BaseForm() {
if (abstractCreateLock)
throw new Error("Can’t instantiate BaseForm!");
}
BaseForm.prototype = {};
BaseForm.prototype.post = function () {
throw new Error("Not implemented!");
}

function GridForm() {}
GridForm.prototype = new BaseForm();
abstractCreateLock = true;
GridForm.prototype.post = function () {
// ...
return "Grid is posted.";
}
window.BaseForm = BaseForm;
window.GridForm = GridForm;
})();
var myGrid = new GridForm();
console.log(myGrid.post()); // Grid is posted.
var myForm = new BaseForm(); // Error: Can’t instantiate BaseForm!

4 接口
在JavaScript中无直接的接口或者虚类支持。但可以由类似下面的代码来模拟,只留一个方法签名检查。

var Interface = function (name, methods) {
this.name = name;
// copies array
this.methods = methods.slice(0);
};
Interface.checkImplements = function (obj, interfaceObj) {
for (var i = 0; i < interfaceObj.methods.length; i++) {
var method = interfaceObj.methods[i];
if (!obj[method] || typeof obj[method] !== "function")
throw new Error("Interface not implemented! Interface: " + interfaceObj.name + "Method: " + method);
}
};
var iMaterial = new Interface("IMaterial", ["getName", "getPrice"]);

function Product(name, price, type) {
Interface.checkImplements(this, iMaterial);
this.name = name;
this.price = price;
this.type = type;
}
Product.prototype.getName = function () {
return this.name;
};
Product.prototype.getPrice = function () {
return this.price;
};
var firstCar = new Product("Super Car X11", 20000, "Car");
console.log(firstCar.getName()); // Super Car X11
delete Product.prototype.getPrice;
var secondCar = new Product("Super Car X12", 30000, "Car"); // Error: Interface not implemented!

5 单例对象:
如果只使用对象的一个实例,可以考虑用单例模式:

var Logger = {
enabled: true,
log: function (logText) {
if (!this.enabled)
return;
if (console && console.log)
console.log(logText);
else
alert(logText);
}
}

或者

function Logger() {}
Logger.enabled = true;
Logger.log = function (logText) {
if (!Logger.enabled)
return;
if (console && console.log)
console.log(logText);
else
alert(logText);
};
Logger.log("test"); // test
Logger.enabled = false;
Logger.log("test"); //
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值