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; };
相关推荐
This book will help you understand the difference between object-oriented programming and protocol-oriented programming. It will demonstrate how to work with protocol-oriented programming using real ...
Chapter 16 - Dynamic Polymorphism—Object-Oriented Programming Part IV - Intermediate Chapter 17 - Well-Behaved Objects—The Orthodox Canonical Class Form Chapter 18 - Mixed Language ...
Key Features Write reusable code that defines and makes objects interact with one another Discover the differences in inheritance and ...Chapter 8: Taking Full Advantage of Object-Oriented Programming
Title: R Object-Oriented Programming Author: Kelly Black Length: 190 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-10-23 ISBN-10: 1783986689 ISBN-13: ...
Chapter 17: Taking Full Advantage Of Object-Oriented Programming Chapter 18: Object-Oriented Javascript Chapter 19: Primitive Data Types, Arrays, Loops, And Conditions Chapter 20: Functions Chapter 21...
Chapter 2: Basics of Object-Oriented Programming Chapter 3: Object-Oriented Features New to PHP 5 Chapter 4: Show a Little Class Chapter 5: Mod UR Class Chapter 6: The ThumbnailImage Class Chapter ...
There are quite a number of PHP books on the market today. What makes this book any different than any other?...Chapter 6: Data Objects Chapter 7: Authentication Chapter 8: Multifunctional Interfaces
The difference between Object-Oriented programming and Protocol-Oriented programming The difference between reference and value types and when to use each How we can leverage tuples to reduce the ...
This book introduces the object-oriented paradigm and its implementation in the Swift 3 programming language to help you understand how real-world objects can become part of fundamental elements in ...
Object-oriented programming is the current cure-all — although it has been around for much more then ten years. At the core, there is little more to it then finally applying the good programming ...
Essential concepts of programming language design and implementation are explained and illustrated in the context of the object-oriented programming language (OOPL) paradigm. Written with the upper-...
C 程序设计教学课件:CHAPTER 1 Object-Oriented Programming.ppt
C++程序设计教学课件:CHAPTER 1 Object-Oriented Programming.ppt
In Chapter 5, Object-Oriented Programming and Chapter 6, Protocol-Oriented Programming we take on this myth by comparing protocol- oriented programming to object-oriented programming to see what is ...
Object-Oriented Analysis and Design for Information Systems clearly explains real object-oriented programming in practice. Expert author Raul Sidnei Wazlawick explains concepts such as object ...
Discover object oriented programming with Java in this unique tutorial. This book uses Java and Eclipse to write and generate output for examples in topics such as classes, interfaces, overloading, ...
Beginning with an explanation of the procedure-oriented programming system, the role played by structures in this system, and the reasons that led to the creation of OOPS, the book provides a ...
Chapter 22-23 UML and C++ : A Partical Guide To Object-Oriented Development 7zip解压,沒有密碼,都是jpg.
Object Oriented Programming with Swift 2 by Gaston C. Hillar 2016 | ISBN: 1785885693 | English | 332 pages Get to grips with object-oriented programming in Swift to efficiently build powerful real-...
Chapter 4 Moving Toward Object‐Oriented Programming . 61 Chapter 5 Controlling the Flow of Your Program . 129 Chapter 6 Handling Exceptions and Debugging. 171 Chapter 7 Delving Further into Object‐...