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 ...
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 ...
Chapter 16 - Dynamic Polymorphism—Object-Oriented Programming Part IV - Intermediate Chapter 17 - Well-Behaved Objects—The Orthodox Canonical Class Form Chapter 18 - Mixed Language ...
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...
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
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 ...
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-...
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 ...
在计算机科学领域,C++是一种强大的编程语言,它在程序设计中引入了面向对象编程(Object-Oriented Programming,OOP)的概念。本课件主要围绕C++编程的面向对象特性展开,旨在深入理解OOP的基本原理和应用。 1.1 ...
C++程序设计教学课件:CHAPTER 1 Object-Oriented Programming 本节课件主要讲解了面向对象编程的基本概念和原则,涵盖了编程语言的发展、面向对象编程的特点、类和抽象数据类型、客户端/服务器模型和消息传递、...
In this chapter we quickly review functions and data structures, as they are building blocks of object-oriented programming. Then we transition into the conceptual definition of objects and classes.
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‐...
Chapter 27: Object-oriented analysis 903 27.1 THE GOALS OF ANALYSIS 903 27.2 THE CHANGING NATURE OF ANALYSIS 906 27.3 THE CONTRIBUTION OF OBJECT TECHNOLOGY 907 27.4 PROGRAMMING A TV STATION 907 27.5 ...