JavaScript notes (vii)

Source: Internet
Author: User

Object oriented

Objects: 1. Program structure describing the properties and functions of a specific thing in reality
property of a thing becomes an Object's property
How the function of a thing becomes an object
2. A piece of storage space in memory that stores multiple data and methods at the same time.
Object-oriented: in a program, you first encapsulate the properties and functions of a thing with an object. The Object's methods are then called to perform the Task.
Why: conform to People's Daily Habits.


How to Use: 2 steps:
1. Create an Object--package: 2
1. Create a separate object:
1. Object Direct Volume:
var obj={
Property Name: Property value,
... : ...,
Method Name: function () {...}
}
When to use: when you create an object, you already know all the properties and methods of the Object.
The properties and methods of the object, collectively referred to as the members of the
Each member name in the object is a string type
however, you can omit the ""
Problem: The Object's method cannot write the property value of the Dead object
Workaround: in the Object's method, direct access to the current Object's own properties
This keyword: used specifically in the Object's methods to refer to the current object itself that is invoking the Method.
is actually called when the method, point "." Before the object
Summary: whenever you access an Object's own properties in the Object's method, you must use This. property name


2. Use the new keyword:
Var obj=new object ();//create Empty objects
={}
obj. Property name = value;
Obj. Method name =function () {
...
}
When to use: when you create an object, you don't know the properties and methods of the object, and you need to add it later.


All objects in JS are associative arrays
Same: 1. Property names are strings and cannot be duplicated
2. Add new properties and methods at any time
3. Use for in to traverse
2. Batch create multiple objects of the same structure


2 steps:
1. Define the Constructor:
Constructors: special functions that specifically define the uniform structure of a class of Objects.
Why: Code Reuse
When to use: in the future, once you create multiple objects of the same structure repeatedly, use constructors to define the uniform structure First.
How to Define:
function type name/constructor name (attribute parameter,..) {
This: the empty object that is currently being created
Adds a new property to the current empty object
This. Property name = Attribute parameter;
Add a new method to the current empty object
This. Method name =function () {
...
}
}


2. Create a new object by calling the constructor with New:
Var obj=new constructor name (attribute value);

New:4 Thing.
1. Create an empty object
2. Set the prototype object for the new Object's __proto__ inheritance Constructor.
3. Call the constructor with the current new object to add properties and methods to the Object.
4. Return the new object address to the variable to Save.
Resolution: inheritance:
Object-oriented three main features: encapsulation inheritance polymorphism
Inheritance: a member of a parent object that can be used directly without having to recreate the child Object.
The inheritance in JS is accomplished by using a prototype Object.
Prototypes and prototype Chains:
Prototypes: saving a class of objects, a parent object of a common member
Why: in order to implement inheritance,
Excellent: code reuse and memory savings
When to use: as long as members common to a class of objects must be centrally defined in the prototype object one Time.
How to Use:
Create: when defining a constructor, JS automatically creates a prototype object of that type
To add a shared member to the prototype Object:
Constructor. PROTOTYPE. member name = value

Prototype objects for built-in Objects:
To resolve browser compatibility issues:
If the required API does not exist in the prototype object of the specified type, the description does not support
It is necessary to add the required common members to the prototype object of the Type.
where, inside the api, you get the object before the point where the API is currently being called

Prototype Chain: The chain that is formed by successive levels of parent objects is the Structure.
Controls the order in which object members (properties and Methods) are used:
Take precedence over the members local to the Object--own property
If there is no local, the prototype chain is only extended to look up all levels of parent Objects. Until it is found. --common attributes
Return undefined if not on the entire prototype chain

To judge a member as its own property, a common attribute:
1. Determine your own properties:
var bool=obj.hasownproperty ("attribute Name")
Determines whether the property name is the own property of obj
Saved locally on the obj Object.
is own property, returns true, otherwise returns false
2. Determine common attributes:
Problem: not own property:
1. may be on the prototype chain
2. It may not be possible at all
Solution: not own, and can access to
!obj.hasownproperty ("property Name")
&&obj. Property name!=undefined

How to modify your own properties and common attributes:
Own property, can only use the object to modify
Common attributes, which must be modified by the prototype object
If you forcibly use a child object, modify the common properties
Consequence: only the current child object is added with the same name as its own property
Cause: the current child object can no longer use common properties
Delete property: Delete Object. properties

Paternity test:
1. Checking with prototype objects
var bool=father.isprototypeof (child)
Determine if child inherits from father
Whether the father is on the Child's prototype chain
Returns true if Father is on the Child's prototype chain
otherwise returns false
2. Check with the constructor function
var bool=child instanceof Constructor
Determines whether the child is a sub-object created by the Constructor.
Complement: instance instance: a sub-object created with a constructor, called a child object, is an instance of a Constructor.
Var Obj=new constructor ();
Instantiate an object of type "constructor"
var obj=new Array ();
var dt=new Date ();
Polymorphism: the same function, in different situations, shows a different state.
Override: If a child object feels that a member of the parent object is not good, you can override the member of the parent object by redefining the member with the same name locally on the child object

Call: forcibly borrowing a function that could not have been called
Target Function. Call (obj)
At execution time: equivalent to obj. target function

JavaScript notes (vii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.