[关闭]
@mSolo 2015-04-29T04:53:49.000000Z 字数 5879 阅读 1667

JavaScript 自学之路(二)

JavaScript


进阶: 面向对象

命名空间

  1. var com = com || {};
  2. com.apress = com.apress || {};
  3. com.apress.chapterone = com.apress.chapterone || {};
  4. com.apress.chapterone.sectionObject = { ... };
  5. var MYAPP = {}; // the container for your application
  6. MYAPP.stooge = {
  7. "first-name": "Joe",
  8. "last-name": "Howard"
  9. };
  10. MYAPP.flight = {
  11. airline: "Oceanic",
  12. number: 815,
  13. departure: {
  14. IATA: "SYD",
  15. time: "2004-09-22 14:55",
  16. city: "Sydney"
  17. },
  18. arrival: {
  19. IATA: "LAX",
  20. time: "2004-09-23 10:42",
  21. city: "Los Angeles"
  22. }
  23. };

创建对象的方法

工厂模式

  1. function createPerson(name, age, job) {
  2. var o = new Object();
  3. // ...
  4. return o;
  5. }

构造函数模式

  1. function Person(name, age, job) {
  2. this.name = name;
  3. // ...
  4. this.sayName = function() { alert(this.name); };
  5. }
  6. var person1 = new Person("Nicholas", 29, "Software Engineer");

原型模式(in 操作符)

  1. // Define a constructor called Accommodation
  2. function Accommodation() {}
  3. // Assign properties and methods to our "class" blueprint with an object literal
  4. Accommodation.prototype = {
  5. floors: 0,
  6. rooms: 0,
  7. sharedEntrance: false,
  8. lock: function() {},
  9. unlock: function() {}
  10. };

组合使用构造函数模式和原型模式

  1. function Person(name, age, job){
  2. this.name = name;
  3. this.age = age;
  4. this.job = job;
  5. this.friends = [“Shelby”, Court”];
  6. }
  7. Person.prototype = {
  8. constructor: Person,
  9. sayName : function () {
  10. alert(this.name);
  11. }
  12. };

动态原型模式

  1. function Person(name, age, job){
  2. this.name = name;
  3. this.age = age;
  4. this.job = job;
  5. //methods
  6. if (typeof this.sayName != function”){
  7. Person.prototype.sayName = function(){
  8. alert(this.name);
  9. };
  10. }
  11. }

寄生构造函数模式

  1. function SpecialArray(){
  2. //create the array
  3. var values = new Array();
  4. //add the values
  5. values.push.apply(values, arguments);
  6. //assign the method
  7. values.toPipedString = function(){
  8. return this.join(“|”);
  9. };
  10. return values;
  11. }
  12. var colors = new SpecialArray(“red”, blue”, green”);
  13. alert(colors.toPipedString()); //”red|blue|green”

稳妥构造函数模式

  1. function Person(name, age, job){
  2. //create the object to return
  3. var o = new Object();
  4. //optional: define private variables/functions here
  5. o.sayName = function(){
  6. alert(name);
  7. };
  8. return o;
  9. }

解析对象

  1. var Person = {
  2. name: "Nicholas",
  3. age: 29,
  4. job: "Software Engineer",
  5. sayName: function(){
  6. alert(this.name);
  7. }
  8. };
  9. var person1 = new Person();
  10. var person2 = new Person();
person1.name = "Greg";
  1. function Person(){}
  2. var friend = new Person();
  3. Person.prototype = {
  4. constructor: Person,
  5. name : "Nicholas",
  6. age : 29,
  7. job : "Software Engineer",
  8. sayName : function () {
  9. alert(this.name);
  10. }
  11. };
friend.sayName(); //error

属性管理器

  1. var car = {};
  2. Object.defineProperty(car, 'doors', {
  3. configurable: true,
  4. value: 4
  5. });
  6. Object.defineProperty(car, 'wheels', {
  7. configurable: false,
  8. value: 4
  9. });
  10. delete car.doors;
  11. console.log(car.doors); // => "undefined"
  12. delete car.wheels;
  13. console.log(car.wheels); // => "4"
  1. Object.defineProperty(car, 'doors', {
  2. writable: true,
  3. configurable: true,
  4. enumerable: true,
  5. value: 4
  6. });
  7. Object.defineProperty(car, 'wheels', {
  8. writable: true,
  9. configurable: true,
  10. enumerable: true,
  11. value: 4
  12. });
  13. Object.defineProperty(car, 'trackingEnabled', {
  14. enumerable: false,
  15. value: true
  16. });
  17. for (var x in car) { // same as console.log(Object.keys(car));
  18. console.log(x); // output: ['doors', 'wheels']
  19. }
  20. // => ["doors", "wheels", "trackingEnabled"]
  21. console.log(Object.getOwnPropertyNames(car));
  22. console.log(car.propertyIsEnumerable('trackingEnabled')); //false
  23. console.log(car.trackingEnabled); //true
  1. Object.defineProperty(car, 'wheels', {
  2. value: 4,
  3. writable: false
  4. });
  5. car.wheels = 5;
  6. console.log(car.wheels); // => 4
  1. ({
  2. maxwidth: 600,
  3. maxheight: 400,
  4. gimmeMax: function () {
  5. return this.maxwidth + "x" + this.maxheight;
  6. },
  7. init: function () {
  8. console.log(this.gimmeMax());
  9. }
  10. }).init();

继承

原型链

  1. function SuperType(){
  2. this.colors = [“red”, blue”, green”];
  3. }
  4. function SubType(){}
  5. //inherit from SuperType
  6. SubType.prototype = new SuperType();
  7. var instance1 = new SubType();
  8. instance1.colors.push(“black”);
  9. alert(instance1.colors); //”red,blue,green,black”
  10. var instance2 = new SubType();
  11. alert(instance2.colors); //”red,blue,green,black”

借用构造函数(伪造对象或经典继承)

  1. function SuperType() {
  2. this.colors = [“red”, blue”, green”];
  3. }
  4. function SubType(){ //inherit from SuperType
  5. SuperType.call(this);
  6. }
  7. var instance1 = new SubType();
  8. instance1.colors.push(“black”);
  9. alert(instance1.colors); //”red,blue,green,black”
  10. var instance2 = new SubType();
  11. alert(instance2.colors); //”red,blue,green”

组合继承

  1. function SuperType() {
  2. this.name = name;
  3. this.colors = [“red”, blue”, green”];
  4. }
  5. SuperType.prototype.sayName = function(){
  6. alert(this.name);
  7. };
  8. function SubType(name, age){
  9. SuperType.call(this, name);
  10. this.age = age;
  11. }
  12. SubType.prototype = new SuperType();
  13. SubType.prototype.constructor = SubType;
  14. SubType.prototype.sayAge = function(){
  15. alert(this.age);
  16. };
  17. var instance1 = new SubType(“Nicholas”, 29);
  18. instance1.colors.push(“black”);
  19. alert(instance1.colors); //”red,blue,green,black”
  20. instance1.sayName(); //”Nicholas”;
  21. instance1.sayAge(); //29
  22. var instance2 = new SubType(“Greg”, 27);
  23. alert(instance2.colors); //”red,blue,green”
  24. instance2.sayName(); //”Greg”;
  25. instance2.sayAge(); //27

原型式继承

  1. function object(o){
  2. function F(){}
  3. F.prototype = o;
  4. return new F();
  5. }
  6. var person = {
  7. name: Nicholas”,
  8. friends: [“Shelby”, Court”, Van”]
  9. };
  10. var anotherPerson = object(person);
  11. anotherPerson.name = Greg”;
  12. anotherPerson.friends.push(“Rob”);
  13. var yetAnotherPerson = object(person);
  14. yetAnotherPerson.name = Linda”;
  15. yetAnotherPerson.friends.push(“Barbie”);
  16. alert(person.friends); //”Shelby,Court,Van,Rob,Barbie”
  1. var person = {
  2. name: Nicholas”,
  3. friends: [“Shelby”, Court”, Van”]
  4. };
  5. var anotherPerson = Object.create(person, {
  6. name: {
  7. value: Greg
  8. }
  9. });
  10. alert(anotherPerson.name); //”Greg”

寄生式继承

  1. function createAnother(original){
  2. var clone = object(original); //create a new object by calling a function
  3. clone.sayHi = function(){ //augment the object in some way
  4. alert(“hi”);
  5. };
  6. return clone; //return the object
  7. }
  8. var person = {
  9. name: Nicholas”,
  10. friends: [“Shelby”, Court”, Van”]
  11. };
  12. var anotherPerson = createAnother(person);
  13. anotherPerson.sayHi(); //”hi”

寄生组合式继承

  1. function inheritPrototype(subType, superType){
  2. var prototype = object(superType.prototype); //create object
  3. prototype.constructor = subType; //augment object
  4. subType.prototype = prototype; //assign object
  5. }
  6. function SuperType(name){
  7. this.name = name;
  8. this.colors = [“red”, blue”, green”];
  9. }
  10. SuperType.prototype.sayName = function(){
  11. alert(this.name);
  12. };
  13. function SubType(name, age){
  14. SuperType.call(this, name);
  15. this.age = age;
  16. }
  17. inheritPrototype(SubType, SuperType);
  18. SubType.prototype.sayAge = function(){
  19. alert(this.age);
  20. };
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注