`
leonzhx
  • 浏览: 770494 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 6. Object-Oriented Programming

阅读更多

1.There is a special variable called this. When the function is called as a method (meaning it is looked up as a property and immediately called, as in object.method()), this will point to the relevant object. The first argument to the apply method, can be used to specify the object that the function must be applied to.

 

2.Functions also have a call method, which is similar to apply, but you can give the arguments for the function separately instead of as an array.

 

3. The new keyword provides a convenient way of creating new objects. When a function is called with the operator new in front of it, its this variable will point at a new object, which it will automatically return (unless it explicitly returns something else using return). Functions used to create new objects are called constructors. It is convention to start the names of constructors with a capital letter. This makes it easy to distinguish them from other functions.

 

4.new does a few things behind the scenes. For one thing, the object created via new has a property called constructor, which points at the function that created it. 

 

5.Every object is based on a prototype, which gives it a set of inherent properties. The simple objects (Typing {}, is equivalent to typing new Object().)  are based on the most basic prototype, which is associated with the Object constructor, and are thus shared by all objects. If an object has another prototype, that prototype is itself an object, which will (directly or indirectly) be based on the Object prototype. You can use a constructor’s prototype property to access this prototype.

 

6.toString is a method that is part of the Object prototype. This means that all simple objects have a toString method, which converts them to a string. In fact, every object has a toString method.

 

7.Every function you define automatically gets a prototype property, which holds an object—the prototype of the function. This prototype gets a constructor property, which points back at the function to which it belongs. Even though objects seem to share the properties of their prototype, this sharing is one-way. The properties of the prototype influence the object based on it, and changes to these objects never affect the prototype.

 

8.When looking up the value of a property, JavaScript first looks at the properties that the object itself has. If there is a property that has the name we are looking for, that is the value we get. If there is no such property, it continues searching the prototype of the object, and then the prototype of the prototype, and so on. If no property is found, the value undefined is given.

 

9.The prototype can be used at any time to add new properties and methods to all objects based on it. 

 

10.Every object has a method called hasOwnProperty, which tells us whether the object (instead of its prototype) has a property with a given name.

 

11.Firefox gives every object a hidden property named __proto__, which points to the prototype of that object. hasOwnProperty will return false for this one. 

 

12.Method propertyIsEnumerable does mostly the same thing as hasOwnProperty. hasOwnProperty will return true even for non-enumerable "own" properties (like length in an Array). propertyIsEnumerable will return true only for enumerable "own" properties. (An "enumerable" property is a property that shows up in for...in loops and such.)

 

13.new Array(x) produces a new array of length x, filled with undefined values.

 

14.Calling a function always results in a new this being defined inside that function, even when it is not used as a method. Thus, any this variable outside of the function will not be visible. A good solution is to use a function similar to partial. Instead of adding arguments to a function, this one adds a this object, using it as the first argument to the function’s apply method:

function bind(func, object) {
  return function(){
    return func.apply(object, arguments);
  };
}

 

This way, you can bind an inner function to this, and it will have the same this as the outer function.

 

15.Objects are turned to strings by calling their toString method, so giving your own object types a meaningful toString is a good way to make them readable when printed out.

 

16.If we make the old prototype object the prototype of the new prototype object it will automatically have all its properties:

function clone(object) {
  function OneShotConstructor(){}
  OneShotConstructor.prototype = object;
  return new OneShotConstructor();
}
Object.prototype.inherit = function(baseConstructor) {
  this.prototype = clone(baseConstructor.prototype);
  this.prototype.constructor = this;
};
Object.prototype.method = function(name, func) {
  this.prototype[name] = func;
};

 

17.Most of the time, a subtype’s constructor should start by calling the constructor of the supertype. This way, it starts with a valid object of the supertype, which it can then extend. 

 

18.instanceof can be used to determine whether an object is based on a certain prototype. You give it the object on the left side and a constructor on the right side, and it returns a Boolean, true if the constructor’s prototype property is the direct or indirect prototype of the object and false otherwise.

 

19.If we only have prototype, We can create a fake instanceof:

Object.prototype.isA = function(prototype) {
  function DummyConstructor() {}
  DummyConstructor.prototype = prototype;
  return this instanceof DummyConstructor;
};

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics