JavaScript notes (eight)

Source: Internet
Author: User
Tags access properties

Custom Inheritance:

1. Modify only the parent object of an object:
Obj.__proto__=father
Problem: __proto__ is an internal attribute and cannot be used directly.
Solve:
Object.setprototypeof (Child,father)
Set Child inheritance Father
Object.getprototypeof (Child)
Get child's Parent object
Problem: You can only modify the parent object of an object at a time
2. Bulk modify parent objects for all child objects:
To modify the constructor's prototype object:
constructor. Prototype=father
Timing: After the constructor has been defined, before the first child object is created.
3. Inheritance between two types:
Problem: Between the two types, there are the same properties and methods
Workaround: Abstract A common parent type, centralizing the same property structure and methods as the two types
How to achieve: 3 steps:
1. Defining parent type constructors and prototype objects
2. Borrowing a parent type constructor in a subtype constructor
How to borrow: Call and apply
The parent type constructor. Apply (This,arguments)
Call and apply: specifically used to borrow a function and replace this in a function with the specified object.
When to use: In the future, whenever this is not desired in the function, it should be replaced with call and apply.

Same: Borrow function, replace this
Different: Call requires that the arguments passed into the function must be passed in independently.
Apply requires that the arguments passed into the function be placed in an array or collection, and the whole passed in
How to use:
function. Call (
The object to replace this, the parameter to pass to the function)

3. Let the prototype object of the subtype inherit the prototype object of the parent type.
Object.setprototypeof (
The prototype object of the subtype,
Prototype object of the parent type
);

ES5:
Properties of the object:
Protection of object properties is provided in ES5
Named properties: What is available. Direct Access Properties
Data properties: Properties that store property values directly


Each data attribute has four characteristics:
Value: Actual Store property value
Whether Writable:true/false can be modified
Whether the Enumerable:true/false can traverse
Configurable:true/false


Whether other attributes can be modified or deleted
How to read the attributes of an attribute:
Object.getownpropertydescriptor (
Obj, "Property name"
)//Returns an object that contains four features
Default value of the attribute:
The four attributes, which are added in the traditional way, are true.
How to set attributes for a property:
Object.defineproperty (obj, "property name" {
Attribute: Value,
...:...
})
Problem: DefineProperty features can be restored at any time
FIX: Modify the feature to set configurable to False
Once the configurable is changed to false, it is not reversible.

Accessor properties: Do not store property values directly, only provide protection for other properties
Internal properties: not allowed to hit. Properties for direct access
Example: Class __proto__

Four features:
Get:function () {return xxx}
Set:function (val) {
Verify Val
xxx=val
}
Enumerable:
Configurable:
When to use: whenever a property is protected with a custom rule, use accessor properties
How to: 2 Steps:
1. Define data properties, actually store property values
Problem: The consumer can manipulate data properties directly, bypassing the protection of accessor properties.
FIX: Closures!
2. Define accessor properties, protect data properties
can only be used with DefineProperty ()
Object.defineproperty (obj, "property name", {
Get:function () {return XXX},
Set:function (val) {xxx=val},
Enumerable:
Configurable:
})
Get/set method does not have to be called manually:
as long as the accessor property value is used, Auto Get ()
When you assign a value to an accessor property, the parameter Val in the auto-set ()
set automatically gets the value to be assigned the accessor property in the
constructor:
Despise: Implement a type, Both public and private private properties are included with the
Private private property: A property that is used only inside an object and cannot be accessed through.
Public Property: Directly with the object. Accessible properties.

Tamper-proof: Prohibit adding, deleting, modifying properties of objects
1. Anti-scaling:
In each object, there is a default internal property:
Extensible, which defaults to True, indicates that new properties can be extended at any time.
If you modify the extensible property to false, the extension of the new property is prohibited
How to modify:
Object.preventextensions (obj)
Block all extensions to the Obj object
Problem: Restrict the addition of new properties, without restricting the deletion of old properties
2. Seal: On the basis of anti-scaling, modify the configurable property of all properties to False
How to seal: object.seal (obj);
Seal the Obj Object
3. Freeze: The value of all properties is prohibited from being modified on a sealed basis.
Object.freeze (obj);

Object.create (): Creates a new object, inherits the specified parent object, and extends the new property for the new object
When to use: Whenever a child object is created based on an existing parent object.
How to use:
var child=object.create (father,{
New properties: {Four features},
...:...
})

Array API:
1. Judge: Whether the elements in the array meet the requirements:
Arr.every (...): Determines whether all elements in arr meet the requirements.
Arr.some (...): Determine if arr contains elements that meet the requirements.
How to use:
var bool=arr.every (
function (Val,idx,arr) {
return judgment condition
}
);
Every will use function to perform a check on each element in arr. Returns true only if the execution result of each element is true. As long as any one does not return true, every immediately returns false.
Description
Val: Automatically get the value of the element currently being checked
IDX: Automatically gets the position of the element currently being checked
Arr: Automatically gets the array object currently being checked
2. Traversal: Perform the same action for each element in an array
Arr.foreach (...): Performs the same action on each element in Arr, and then saves it back to the original array.
Modify the original array directly
Arr.map (...): Takes out the value of each element in Arr, performs the same action, and then saves it to the new array
Do not directly modify the original array, return the new array

3. Filter and Summarize:
Filter: Filters out the qualifying elements in Arr to form a new array.
var sub=arr.filter (function (Val,idx,arr) {
return condition
})
The original array is not modified.

Rollup: counts all the elements in the current array, one result.
var r=
Arr.reduce (Function (prev,val,idx,arr{
Return adds Val to the result in Prev
},base)
Each value in Arr is rolled up into prev in turn.
Description: Prev obtained a temporary summary of all element values prior to the IDX position.
Base, which sets the summary value for the start.

2.bind
Bind: Creates a new function based on an existing function, while permanently binding the This object and some parameters.
Why:
Call and apply:
Temporary borrowing functions, replacing the this one, passing in parameters.
Issue: Temporary borrowing, temporary replacement of this
FIX: Bind
When to use: when this and some parameters are permanently bound
How to use:
var fun=fun.bind (obj, parameter 1, parameter 2,...)
Based on the existing fun function, return a new function, and permanently bind the new function in this is obj, while binding part of the parameter value is parameter 1, parameter 2,

3. Strict mode:
Enable: Code Snippet Top: "Use strict";
2 places:
The beginning of the 1.script
2. Start of function
Rules:
1. Fail silently, upgrade to error
2. Prohibit assigning values to undeclared variables
(function () {"Use strict";
m=12;//Error
})();
Console.log (m);//12
3. Disable the Arguments.callee
(function () {"Use strict";
Argument.callee ();
})();
4. More than the Eval scope
(function () {"Use strict";
Eval ("var sum=2+3");
Console.log (sum);
})()

JavaScript notes (eight)

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.