Object Oriented (JS)

1. What is the object of
a collection of objects is key to children's disorder.

Everything in JavaScript objects.
Any object has its own properties and methods.
All objects are possible new
all through the new objects are called keywords derived instance of an object.
STR new new String = var ( "ABCDE");
var num1 = Number The new new ( "123")

The method of creating an object 2
1. literal
2.New Object ()
3. Custom constructor
4.Object.create () es2015 new method
5. factory function

3 factory function: mass objects
chestnut: createStudent function (name, Age, className) {
var obj = new new Object ();
obj.name = name;
obj.age = Age;
obj.className = className;
obj.study function = () {
the console.log ( "learning Css")
}
return obj;
}

Benefits: Direct calls can generate objects createStudent ()
drawbacks factory function: each call factory function, have opened up space in memory
all functions similarly regenerated, resulting in a waste of memory.

4. The origin of self-defined constructor
factory function has a memory problem of waste, in order to solve the waste problem of memory, there is a custom constructor.

5. Custom constructors: mass action on the object
specified: capitalized
function Dog (name, Age, Color) {
this.name = name;
this.age = Age;
this.color = Color;
this.say function = () {
the console.log ( "Hello")
}
}
var = damao new new Dog ( "ovo", 12 is, "Yelow")
new new custom constructor
what happened in the memory?
1. open space to create a new object in memory. this points to this object.
2. All properties and methods of the constructor, to copy all of the new object
3. Each constructor function has a prototype object, like on the new prototype object also inherits all the
attributes and methods.


Custom constructor, the advantage is to avoid each time you create an object, it opens up space in memory
to generate the same function. Save memory.


6. The built-in objects also commonly used within the constructor function built
Number The String Boolean
the Array the RegExp a Date the Math
Function Object
Free Join Error

7. What is object-oriented
object-oriented is a thinking, problem-solving is to focus on the object.

Object-oriented has three characteristics: a package inheritance polymorphism
Package: all the properties and methods associated in a package object.
Inheritance: I own no property or method, to someone else's use of them.
Polymorphism: different objects according to the circumstances of the incident, a different code execution.
js it can be simulated. arguments.
8. The prototype object
to the same properties and methods written in the prototype object's constructor function.
Examples of object constructor out of new instances on all accessible.
The maximum effect of the presence of the prototype object is to save memory space
9. prototype chain
*** prototype chain: relations step by step by the __proto__ inherited form objects at all levels

The role of the prototype chain also save space.
Implementation:
1 to establish a base object constructor, then create an inherited object constructor,
2 and object inheritance with prototype.



Prototypal inheritance
10. Analog inherit call apply bind
analog inherit actually perform a function, the implementation of this change has become the current instance of the object constructor
does not save memory space.
Chestnut
function the Person (name, Age) {
this.name = name;
this.age = Age;

}
Person.prototype = {
say: function () {
the console.log ( "Hello, the I'm" + this.name)
}
}

function Student (name, Age, studyObj) {
analog succession
Person.apply (this, arguments);
corresponds
this.name = name;
this.age = Age;
}

11. Complete prototype chain verification
// Person prototype object of the __proto__
the console.log (Person.prototype .__ proto__ === Object.prototype)

// Constructor Function Person is an instance of an object
console.log (Person .__ proto__ === Function.prototype)

// Constructor Function Object prototype object is an instance of an object
console.log (Function.prototype .__ proto__ === Object.prototype)

// Object instance of the Function constructor.
console.log (Object .__ proto__ === Function.prototype)

// constructor Function is its own instance of an object
console.log (Function .__ proto__ === Function.prototype)

// fragmented
the console.log (Object.prototype .__ proto__ === null)
// arbitrary objects are instances of Object
var = {O}
the console.log (O .__ proto__ === Object.prototype)
// arbitrary function examples of function objects are
var F = function () {}
the console.log (F .__ proto__ === the Function.prototype)

12. classical inherited
the Object.create ()
var = xiaohei the Object.create (CAT)
xiaohei prototype object is CAT
the console.log (xiaohei .__ proto__ == CAT)
var = duolaBmeng the Object.create (CAT, {
name: {value: "Dora B dream"},
eAT: {value: function () {
the console.log ( "I love gyro burn")
}}
})
13. inherited, mixed
function Extend (OBJ1, obj2) {
for (var K obj2 in) {
OBJ1 [K] = obj2 [K]
}
}

14. The built-in objects to (constructor) extension method.
IF (! Object.prototype.extend) {
Object.prototype.extend = function (obj2) {
for (var K in obj2) {
the this [K] = obj2 [K];
}
}
}
However, in order to avoid conflicts, to the built Object (constructor) minimize the use of extension methods.
So if you need to expand the team within the company, some good discussion, to avoid conflict


15. String Array built configured to extension method

 

 

 

Guess you like

Origin www.cnblogs.com/yryx/p/11361846.html