日期:2014-05-16 浏览次数:20449 次
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
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
(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!
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, "C