Object-oriented programming ideas ------ ---------- abstract process instantiation process

Object-oriented programming ideas abstraction ------ ---------- instantiation process
a) according to the requirements, the relevant abstract objects
2 summarizes the characteristics and behavior of objects), the feature into properties, the behavior becomes method
3) define a constructor, instantiate an object
4) by calling the object properties and methods, to complete the respective requirements


Process-oriented: everything should be hands-on, process know everything, pay attention to the process of
object-oriented: find objects on demand (packaged function) everything to do with the object, focusing on the results, only the process is to call the
object-oriented features: encapsulation, inheritance, polymorphism
JS is not object-oriented language, but can be simulated object-oriented thinking
JS is an object-based language, JS has many built-in objects, JS can simulate object-oriented, JS can create objects
JS no inheritance and polymorphism, but can be simulated
package refers to the code in one place
what is an object, especially something with specific properties and methods specific to code collection
in the current object's methods you can access the current value of the object's properties, you can use this value, the current value of
the object this always points to the current method call or property
if an object is passed as a parameter into a function to operate, then no return statement also can access an object manipulated
1: creation of objects in memory space inside:
1) either through Object forms created in memory of the form is the same, the object assigned to the variable
2) the object instance on the heap, there is an address, and the variable is stored in the memory stack inside, and inside the stack
to store the object instances in the "stack" inside the address of
1: three ways to create objects
1: call the system constructor creates an object
type object system comes, like the Array, a direct call to create the object
var obj = new object (); // variable storage object, called object instance, obj is the "instance" of an object type, it means a case specific to
add the attribute value obj.name =
Method of adding function obj.name =
instanceOf for judging whether a variable type
how to create multiple objects
to create an object code function encapsulated in a
function createObject (an attribute value, the attribute value 2,) {
var obj = Object new new ();
obj.name = "attribute value 1"
obj, Age = "attribute 2"
return obj
}
2: custom constructor creates an object (in conjunction with creating a first binding factory mode)
function the Person (attribute values 1, attribute value 2,) {
this.name = "attribute value 1";
this.age = "attribute value 2";
}
var = new new xiaoming the Person ( "bright", 22);
Note:
the difference between the functions and constructors 1) : whether the first letter capitalized
2) constructor to create an object, but can also be used as a normal function
3) constructor function replaces createPerson not explicitly create objects directly assign properties and methods to this subject, there is no return statement
advantages:
with respect to the factory mode, the custom constructor means that the future it can be identified as an instance of a particular type, it plainly, is easy to distinguish
and to create an object diversely than I want to create an object of type person, we can construct a Person (person) constructor, which
Properties and methods defined and who is fit, I want to create an object such as the type of animal, we can construct an animal (animal) constructor
inside the properties and methods and animals are fit,
Note: Of course, any functions can be used as a constructor, only need to add new call on the line
creation process:
1) open (application for a free) space in memory, object store newly created
2) put this set to the current objects (instances object)
properties and methods 3) provided object
4) to return this object
5) into the variable final

use uppercase letter default
3: Create object literal way
of example:
var NUM = 10; // assignment literal form
var arr = []; // creates an array literal form (blank)
var obj = {}; // Create Object literal form (blank)
syntax:
var obj = {
name: "Bob",
Age: 20 is,
EAT: function () {.....};
}
Ps of: intermediate, separated by commas
4: instantiating a plurality of objects to solve the problem in the method of recreating the same function by the introduction of the prototype of
such ten thousand instances of the Person object is created ten thousand phase function , But the methods are not equal, a waste of memory, but the method is defined in
the outside, so that such
function eat () {console.log ( " xx")};
the Person function (name, Age) {
this.eat = EAT;
}
easily be replaced with the same name variables, so that we can reference to the prototype to solve this problem:
Person.prototype.eat = function () {console.log ( "xx" )};
function the Person (name, Age) {
this.eat = EAT;
}
this perfect, and there is no instance of the object of this method, only by pointing to an object instance __proto__ property prototype prototype object in
the eat () method, so you can use
4: another way to access the object properties
syntax:
obj [ "attribute name"]
obj [ "method name"] ();
5: summary
defects create an object literal: can not pass value, literally the way to create objects amount improved version of the constructor creates an object
constructor to create an object flaw:
when the same type of instance object is not equal to invoke methods obj1.eat obj2.eat//eat is a self-defined constructor method of
each different instance a method which is not equal to the object, when there are multiple instances of objects, is relatively wasteful of memory

6: Relationship between structure and function and the prototype object instance of the object
1) is used to instantiate an object constructor, the constructor function itself does not include properties and methods, properties which are instance objects
2) which has a constructor prototype object prototype, which stores the method, there is a constructor property: constructor
the key thing is to create an instance of an object, which points to the constructor itself;
3) instantiate an object stored inside their own property, but not stores method, but each instance of an object in a __proto__ property,
which points to the constructor of the prototype object, which stores the way, each object instance is one such method is invoked, the object instance
can directly access the inside of the prototype method
7: shared data using the prototype
1) addition method, attributes may be written in the prototype
syntax:. Object.prototype shared attribute value =
2) is the prototype objects, it is possible to replace the literal wording wording:
Object.prototype = {
constructor: Object, // points to manually
shared attribute 1: 1 value,
shared properties 2: 2 value,
Party . 1: function () {.......}
};
Note: This must be manually constructor constructor point, otherwise there is no prototype
8: The method can call each instance of an object
Syntax:
this.play = function () {this.eat ()}
this.eat = fucntiong () {. . . . . . . . . }
Also, the method which can access each prototype
syntax:
Object.prototype.eat = functiong () {this.play ()}
Object.prototype.play = function () {. . . . . }
9: sequentially instance object properties and methods of the same name
example:
function the Person (Sex) {
this.sex Sex =;
}
Person.prototype.name = "F";
var = new new per the Person ( "M");
Console. log (per.sex) // M
Description:
when calling this attribute, the system will start with the instance of the object inside to find, if not, then to the prototype object inside to find, this is called the prototype chain, the method is the same
10: Add as built-in objects prototyping
Array () String () Date ( ) these are the system comes with a constructor, when we instantiate them and call the method, they found instance of an object
method calls are placed to the prototype object of these basic constructor (prototype ) in
1) that if we could add a method to the prototype object system it (changing the source code)
For example, I would like to have a reverse string string method
String.prototype.mymath = function () {....}
this is a
11: prototype object adds an attribute
syntax:
function the Person () {
= 0 this.a;
}
Person.prototypr.aaa = function () {
this.a = 100;
}
var = new new per the Person
Person.aaa ()
the console.log (per.a) // 100
Description: The following declaration to call the property to take effect after
12: bind method to change the function of pointing
syntax: Game.prototype.automaticRun = function () {
var that = the this;
this.snake.init ();
setInterval (function () {
// use that here Game point to objects, the this window is pointing to the object, an object belonging to the window of the timer is
this.snake.move ();
this.snake.init ();
} .bind (that), 150); // bind here the method of this point can be changed
};
13: another method of writing attribute called
var = new new per the Person ()
per [ "attribute name"];
per [ "method name"] ();
14: prototype points may vary
examples of the prototype object (the __proto__) pointing prototype object constructor (the prototype), but the prototype object constructor can be changed, because the constructor
Itself is a target to be written in the form of a literal, the prototype object constructor may point to another, and examples of the object points on the failure of the original
syntax:
// define a constructor
function the Person (Age, name) {
the this. active = "repeater",
this.age = Age,
this.name = name
};
// prototype object Person of
Person.prototype = {
of the type: "human",
EAT: function () {
console.log ( "I want to eating ");
},
constructor: the Person
}
// define another constructor
function Student () {

};
// constructor prototype objects pointing to another instance of the object constructor
Student.prototype = new Person (12, "slag slag");
// instantiate this constructor
var STR = new new Student ();
the console.log (str.name); // slag slag
understood: the prototype of this object is a pointer to another constructor constructor By way of example, it called the prototype chain, we can imagine, we define a human subject,
Which defines the basic human attributes and methods, then we define a student's constructor, because they essentially belong to human beings, so we will prototype
object points to it, so that the students examples of objects you can access to people some properties and methods
Description: __ proto__ point constructor prototype, the prototype object has __proto__ which, while pointing to the prototype prototype __proto__ a constructor
so
15: Object of the prototype of the prototype object is directed __proto__ null
understand: constructor has a prototype object prototype, and the prototype object is an object that has its own prototype __proto__, point to Object object,
and the object is the Object prototype Object itself, while the prototype object prototype of the object prototype __proto__ It is null;
summarize: any object instance is Object, and Object is the end of the prototype chain


16: Prototype point change also how to add your own prototyping normally do ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
we only need to change the point can be added after the prototype method
syntax:
Student.prototype new new = the Person (12 is, "slag slag");
Student.prototype.sayHi = function () {
the console.log ( "handsome, you okay");
}
Description : in fact, this manner does not change back to point or point to two directions at the same time, the method appears to be defined in its own prototype object,
the solid sucked method is defined in the another instance of the object constructor
Student.prototype .sayHi equivalent
new Person (12, "slag slag") .sayHi = function () {} ......
the PS: writing abstract
17: package
package: packing is
for example: a value stored in a variable
number of repeated function code in a
set of attributes and methods in objects in a
number of objects in a file js
18: polymorphism
of an object with a different behavior, or is the same behavior for different objects, different results , state how you want, you have to first have inherited
19: class
of object-oriented programming language There class concept (class) of (a particular data type)
20: Inheritance
Inheritance is a relationship, the relationship between classes in the class, because there is no JS class, but can be configured to simulate the function class, and is achieved by a prototype inheritance
inheritance for data sharing, JS Inheritance also to realize data sharing
effect of prototypes 1: data sharing, saving memory space
action prototype 2: in order to achieve inheritance
Inheritance is a relationship:
the relationship between parent and child level class level
, for example: the parent category must have some subset of categories, some subset of the parent category level is not necessarily
21: prototype chain to inherit the problem
syntax:
Son.prototype = new new father (agu1, agu2);
we assign the child constructor prototype constructor of the parent by example, this has led to property indeed inherited, but common the property value of all the same
22: borrow constructor inherited
this we can change the child by the constructor method call directed
syntax:
function son (agu1, agu2, agu3) {
Father.call (this, agu2, agu3)
this = scroe .scroe;
}
analysis: this refers to the Person Son of this point to point, i.e. son instance of an object, the person with respect to instantiate Son instantiation in
constructor .call (current object, attributes, properties, attributes)
// attribute inheritance solved, and the value will not be repeated
@ defects: The method of the parent category not inherit
23: call method Detailed
word, change objects this point
Popular point: I want to use a method other objects
Syntax:
Other object method name .apply (the current object, parameters, .......);
syntax
such as function a () {.....} } = {b
a.call (b)
to give
b = {
function a () {} .....
}
parsing of: a is this (window) points b, b into a function equivalent to the object, so that points to a function object b
Note that a function is an object, is a reference to a function object, to add an object that is placed under obj let add become the object obj property or method
but it does not really exist in obj object, used to delete
one, if not the incoming call and apply parameters or pass null, then call the method functions this points to the default window
two, apply and call the method passed in the second and later parameters are parameters of the method call object, such as a.call (obj, arg1, arg2) , which is a arg1 and arg2 object
parameters, and the two methods can be used to call the function, if the function has a return value, call the method return values can also be entered in the form of an assignment of
three, apply and the only difference is that the incoming call parameters Form, using an array Apply passed parameters, call using literal form
four, Apply and call this function or object point change, the parameter points to the first object, but does not really belong to the object pointed to,
five, two function constructor method is present in the prototype of the prototype
24: combination Inheritance
prototype inheritance + borrow constructors Inheritance
Syntax:
function Son (agu1, agu2, agu3) {
Father.call (the this, agu2, agu3)
this.scroe = scroe;
}
Son.prototype Father new new = ();
parse : call call () constructor inherited borrow implemented prototype method can not inherit the parent class, so the prototype chain we inherited methods can be combined, and there is not an instance of the
need to pass parameters, just to use the prototype method (as it has been by call parent class inherits attributes)
25: copy inherited
to which an object properties and methods of another object onto which traverse through the loop
function Person () {

}
Person.prototype.name = "Millet";
Person.prototype.age = 33 is;
Person.prototype.sex = "F";
Person.prototype.sayHi = function () {
the console.log ( "ah ha")
}
/ / null object defined
var jse = {};
for (key in Person.prototype var) {
// set dynamic Forin cycle to jse key property and assignment
jse [key] = Person.prototype [key];
}
26 is: summary Inheritance
prototype role: data sharing, saving space
inherited role: data sharing, space-saving
prototype chain to inherit: change point to the prototype, shared property values can not be changed
borrow constructors Inheritance: the main problem property value can not be changed, but you can not not inherit prototype object
composition inheritance: the prototype chain inheritance + borrow constructor inheritance, property value, and can not solve the problem of succession prototype object
copy Inheritance: is the object properties and methods, copying it to another method of traversal object
27: the same name There are issues of prototype properties and attributes
sentence: If the internal attribute no value, then the prototype property of the same name will overwrite the internal properties, if the internal attribute assignment, then the original of the same name Property will not work!
28: Advanced Functions
function declaration of
function f1 () {}
f1 ()
function expression
var F2 = function () {}
F2 ()
Note: If the function declaration in if-else statement, IE8 problem occurs in the browser, which IE8 else statement will also preresolvable. .
But the function expression can be solved by way of the issue, the first-defined functions, and then into the if-else statement in judge assignment problem does not occur in IE8
29: bind () method as well as the other two methods and case distinction
a, bind function is similar to the method apply (), call (), used to change the object's return to this point but is a function of, if the call need to add a parenthesis,
like this: obj.bind () ()
two the syntax: obj.bind (targetObj, arg1, arg2 ), the first argument passed when null point of this default window
Third, the biggest difference bind () method and call (), apply () is the bind () method copy function will not execute, call and apply directly execute functions, typical case is
the setting timer function point is to use the bind timer can be used normally, but with call and apply, although this can be changed point, but only one output
30: Some of the attributes function
name: name of the function, read-only, can not be modified
arguments: the number of arguments
length: the number of formal parameters
caller: calling function the function
31: Higher Order letter It functions as arguments
First, when the function as a parameter, the incoming call will be executed normally can not be bracketed, when the brackets, provided that the return value of the function (function returns a value premise parameters)
II.

32: Scope scope chain, the pre-analytical
variables: local and global variables
scope: variable region may be used
block-level scope: variable region can be used, refers to a pair of braces {} stuff inside, JS no block-level scope
scope chain: internal scopes can use external data layers find
preresolution: declaration of variables, function declarations (including the inside of the code) will be raised to the uppermost current scope assignment, variables, expressed as a function formula assignment same position
33: closures
have function closures, closures and the object, almost a meaning
analysis: mainly based scope chain to complete, in the cache data for a non-local variable with global scope defined
syntax: 1 or local variables declared within the code ------> local variable, the data cache
internal function returns a function of 2 variables used inside, and the scope of a call the function
3 each use the return value (function ) will initialize a variable, which would extend the release time --------> memory for caching data
advantages: cached data, disadvantages: cache data, Fees space
34: sandbox, sandbox
virtual environments, which run the code, will not affect the outside (to avoid duplicate names conflict), equivalent to a virtual test environment
to achieve: Use local variables, from the calling function
syntax:
var NUM
(function () {
var NUM
and a series of code enclosed environment, and do not conflict with the outside world
} ())
Analysis: we can call a function from inside as a closed environment, our custom code and run inside As the scope of the relationship and the outside world will not conflict
Usually I do not want to write code that can be affected (such as duplicate names) code blocks are placed inside the sandbox, if you want to spread variable, you need to manually set the properties window
35: a recursive
function call the function yourself, must be ended conditions
syntax: function f1 () {
execute code
f1 ()
end condition avoid infinite calls
}
f1 ()
Note: Do not use recursion, inefficient
36: shallow copy of
the entire property of a replicated object to another object
deep copy : not only the method of replication properties, but also copy the new object
37: pseudo-arrays and arrays
arrays are instance objects constructor array, an array of instances of the Object Object pseudo constructed to simulate the array index by attribute name, to lei'si
1: array has length property, no dummy array
2: an array of variable length, the length of the dummy array immutable (dynamic access length means length)
3: array array can be reduced using a method, with the dummy array prototyping of non array

Guess you like

Origin www.cnblogs.com/loveHtml/p/12102560.html