Most of the time we are in the process-oriented, are just the people who use the object, now learning object-oriented is to become the person to write (transform) the object.
first, what is the object
- Objects: Collections of properties and methods
- Attribute: is actually a variable (but there is a attribution)
- Method: is actually a function (but there is a attribution)
- Class: A collection of objects of the same set of properties and methods
Second,
Third, newFactory mode:1, a person's class:
2. Do not use new to create an object for a person
constructor Mode:1, a person's class:
function Person (name,sex) {// constructor mode this. name=name; this. sex=sex; this. showname=function () {alert (this. name);} this. showsex=function () {alert (this. sex);}}
2. Create a new person's object with new
var ben=New person ("Ben", "false");
Iv. prototype prototype
- Format: Type. prototype. properties (methods) = parameters (function functions);
- Type: can be array\object\string, etc.
- Properties: Customizable
- Function: Binds a property (method) to a variable or attribute of all corresponding types, which must be called by the
v. instanceof Judgment
- Function: Used to indicate whether an object is an instance of a particular class;
- Format: Object instanceof Class
- Object is arr1, str2, Array ... such as
- Class is an Array, Object, String ... such as
Six, the prototype chainExample: Var arr=[1,2,3];alert (Arr.index);
- Undefined is also a value
- 1. First, you will see if ARR has the index attribute on this variable.
- 2.Array this class (constructor) has no index this property
- 3.Object This class has no index this property
- 4. Return undefined and assign values to arr
Vii. Inheritance of apply && call1. Create a human: function person (name,sex) {this.name=name; This.sex=sex; This.showname=function () {alert (this.name); }} 2, define a worker class (see the difference between call inheritance and apply inheritance): 3, Difference:
- When you use apply, the parameter list location must correspond to:
- function person (name,age) {}
- Student (name,age, sex) {}
- When apply is passed
- Person.apply (this,arguments);
- When using call, the parameter list position may not correspond :
- function person (name,age) {}
- Student (age,name, sex) {}
- But the location corresponds to when the call is delivered .
- Person.call (This,name,age);
Note:Call, apply change this point
- Call: Parameters in order
- Apply: A second argument when an array arguments
JS Object-oriented