JavaScript object-oriented (upper)

In JavaScript, the data type is divided into two categories:

Original type

Save some Simple data, such as True, 5, etc. JavaScript has a total of 5 original types:

Boolean: Boolean, value is true: False

Number: Digital, value is any integer floating point value
String: string, value is Single characters or continuous characters of quotation marks or double quasks (JavaScript non-distinguishabra type)
null: Null type, only one value: nulll
undefined: Undefined, only one value: undefined

VAR name = “pomy”; var blog = “”; var Age = 22; Alert (TypeOf Blog); // “string” Alert (TypeOf agent); // “Number”
 The value of the original type is stored directly in the variable and can be detected by TypeOf. But TypeOf's detection of NULL returns Object instead of returning null:   
// pops up NULLIF (TypeOf Null) {Alert (“not null”);} Else {Alert (“null”);}

So when NULL is detected, it is best to be equivalent to (===), which can also avoid mandatory type conversion:
 Console.log ("21" === 21); // FalseConsole.log ("21" == 21); //trueconsole.log (undefined == null); //trueconsole.log (undefined === null); // false   
For strings, numbers or Boolean values, these methods come from the corresponding original package type: String, Number, and Boolean. The original package type will be created automatically.

VAR name = “pomy”; var char = name.charat (0); console.log (char); // “p”

VAR name = “pomy”; var temp = new string (name); var charr = Temp.Charat (0); temp = null; console.log (char); // “p”

The reference to the string object is immediately after using it. Destroy, so you can’t add an attribute to the string, and the instanceOf detects the corresponding type to return false:
var Name = “pomy”; name.age = 21; connection. Log (Name.AGE); ///undefinedConsole.log (Name InstanceOf String); // False

Reference type

Save as object, essence is pointing to memoryThe reference is set, so the object is not saved in the variable. In addition to custom objects, JavaScript provides 6 in-squares type:
  array: array type, a set of ordered lists of numbers index  Date: Date and Time Type 
Error: Operation error type function: Function: Function: General Object: General Object Type
Regexp: Regular Expression Type

can be used to instantiate each object, or use a literal form to create Object:

VAR OBJ = New Object; var = {Name: “Pomy”, Blog: “”, “My Age “: 22}; console.log (; // access attribute console.log (own [” my age “]); obj = null; // dereferencing
OBJ does not include an object instance, but a pointer (or reference) pointing to the location of the actual object in the memory. Because TypeOf returns Object for all non-function reference types, you need to use InstanceOf to detect the reference type.

In JavaScript, the function is an object. The deterministic feature that makes functions is different from other objects is that there is a function that has an internal properties called [Call]]. The internal properties cannot pass code access but define the behavior of code execution.

to create the form

1, the function declaration: a function key, the context is up to

2, function expression: can not be lifted
3, examples of the Function Type

SayHi ();// Function enhances function sayhi () {console.log (“hello”);} // Other equivalent equivalent mode / * var sayhi = function () {console.log (“hello”);} var sayhi = new Function (“” Console.log (\ “);”); * /

 JavaScript function Another unique is that any number of parameters can be delivered to functions. The function parameters are saved in the Arguments class number object, which automatically exists in a function, can reference the parameters through the digital index, but it is not an array instance:   Alert (array.isarray (arguments)); // false 
Class array object arguments saves the function of the function, but does not ignore the shape. Thus, arguments.length returns the length of the real-reference list, and arguments.callee.length returns the length of the design table.

Function Ref (Value) {RETURN VALUE;} console.log (Ref (“hi”)); console.log (Ref (“hi”, 22)) Console.log (Ref.Length); // 1

About this problem, Refer to this article: This article in JavaScript.

JavaScript provides three methods for changing this point of interest: Call, Apply, and Bind. The first parameters of the three functions are the value specified by this, and other parameters are passed to the function as the parameters.

 Object is a reference type, two ways to create objects: Object constructor and object word form:   
VAR Per1 = {Name: “pomy”, blog: “”}; var per2 = new object; = “Code far from” not written “;

In JavaScript, an attribute can be added to the object at any time:

 Per1.age = 0; per1.SAYNAME = function () {alert (; // "pomy"}   
When the detection object attribute is present, a common mistake is:

// The result is falseif (per1.age) {alert (true)} else {Alert (false

Per1.age is present, but its value is 0, so the IF condition cannot be satisfied. The value in the IF judgment is an object, a non-empty string, a non-zero digital or TRUE, and it is determined that the value is evaluated as true; while the value is a null, undefined, 0, false, nan or empty string evaluation as a false.
Thus, when the detection attribute is present, there are additional methods: IN and havenProperty (), the former detects the prototype attribute and its own (instance) property, the latter only detects its own (instance) attribute .

Console.log (“AGE “in Per1); //trueconsole.log (” “)); //trueconsole.log (“tRueConsole.log (“tRueConsole.log (“try.hasownproperty”) ; // false

Object Per1 does not define TOSTRING, which inherits the object.Prototype, so IN and HASOWnProperty () detects differences when this property is detected. If you just want Judging an object attribute is not a prototype, which can be utilized:

 Function isprototypeProperty (Obj, name) {Return Name In obj &&! Obj.hasownproperty (Name);}   
To delete an attribute, use the delete operator to delete the own attribute, and cannot delete the prototype attribute.

Per1.toLog (“Per1 Object”);}; console.log (“toString”)); //trueper1.toString (); // “Per1 object “delete per1.tostring; console.log (” toString “)); //falseconsole.log (per1.tostring ()); // [Object Object]

Sometimes you need to enumerate an enumeration property of an object, and there are two ways: for-in loop and object.keys (), the former still traversed the prototype attribute, and the latter only returns its own attribute. The value of all the internal properties of all enumerated properties[[Enumerable]] is True.
   VAR PER3 = {Name: "Pomy", Blog: "", AGE: 22, Getage: Function () {Return 

In fact, most of the values ​​[[Enumerable]] of[[Enumerable]] is false, ie the attribute cannot be enumerated. Can detect if the property can enumerate via the Propertyisenumerable ():

 Console.log (Per3.Propertyisenumerable ("name")); // truevar pros = Object.keys Per3); // Returns the name of the name of the enumerable property console.log ("Length" in pros); //trueconsole.log(Pros.Propertyisenumerable ("length ")); // false  
The attribute name is a custom, enumerated; attribute Length is the built-in attribute of Array.Prototype, cannot be enumerated.

Attribute type

There are two types: Data Attributes and Accessor Properties. Both have four attribute characteristics:

Data attribute: [Enumerable]],[[Configurable]],[[Value]] and[[Writable]] AccessMerger properties:[[Enumerable]],[[Configurable]],[[Get]] and[[SET]]
 **[[Enumerable]]: ** Boolean value, if the property is enumerated, customize The property is True by default.  **[[configurable]]: ** Boolean value, whether the attribute is configurable (modified or delete), the custom property is true. It is irreversible, then set to true, then set it to TRUE.  **[[value]]: ** Save the value of the property. 
**[[w w]]: ** Boolean value, whether the attribute is writable, all attributes can be written by default. **[[GET]]: ** Gets the attribute value.
**[[[set]]: ** Set the value of the property.

ES 5 provides two methods for setting these internal properties:

Object.defineProperty (Obj, Pro, DESC_MAP), and Object.defineProperties (Obj, Pro_MAP). Using these two methods to add an attribute for Per3 and create a new object Per4:
Object.defineProperty (Per3, “SEX”, {Value: “Male”, ENUMERABLE : false, configurable: false, // Attributes cannot be deleted and modified, this value cannot be set to true}); console.log (; //’male’console.log (PER3.Propertyisenumerable (“sex “)); // falsedelete; // You can not delete =” female “; // can not modify console.log (; //’male’Object.definePrOPERTY (Per3, “SEX”, {Configurable: true, // error}; per4 = {}; object.defineproperties (Per4, {name: {value: “dwqs”, Writable: true}, blog: {value: “”}}}}}}}} {Return;}, set: function (value) { = value;}, enumerable: true, configurable: TRUE True}}); console.log (; // = “pomy”; console.log (; // pomy
It is important to note that when the new properties are defined by these two ways, if the feature value is not specified, the default is False, and the attributes with simultaneous data feature and accessor feature cannot be created. You can get a description of the property feature by the Object.GetownPropertyDescriptor () method, accept two parameters: objects and attribute names. Returns the properties description object if the property exists.

VAR DESC = Object.getownPropertyDescriptor (Per4, “name”); console.log (desc.enumerable); //falseconsole.log(desc.configurable); / / felseconsole.log(desc.wRitable); // true
According to the attribute type of the attribute, the returned attribute description object contains its corresponding four attribute features.
  It is forbidden to modify the object  
of the object and attributes to guide internal features of its behavior. Among them,[[EXTENSIBLE]] is a Boolean value that refers to whether the object itself can be modified ([[EXTENSIBLE] value is true). The object created is default that can be extended, and new properties can be added at any time. ES5 provides three ways:

Object.preventextensions (OBJ): Create an unlatable OBJ object, can use Object.isextenSible (OBJ) to detect whether OBJ can scale. Strict mode is added to the non-extended object to add an error, and the addition failed in non-strict mode.

Object.seal (obj): Seal object, at which time the property of the OBJ becomes read-only, it cannot be added, changed, or deletes the property (all attributes are unconfigured), and its[[EXTENSIBLE]] value is false,[[[[” Configurable] The value is false. You can use Object.Iseled (OBJ) to detect whether OBJ is seal.
Object.Freeze (OBJ): Freezing objects, cannot add or delete attributes on the freezing object, and cannot change the property type, and no data type is written. Object.isfrozen (OBJ) can be used to detect whether OBJ is frozen. Note: The frozen objects and seal objects are used in strict mode.
   "Use Strict"; var PER5 = {Name: "pomy"}; console.log (object.isextensible (per5)); //trueconsole.log(Object.isextensible .Ssealed (Per5)); // FALSeconsole.log (Object.isfrozen (Per5)); //falseObject.freeze (Object.isextensible (Object.isextensible(Per5)); //falseconsole.log (Object.Isealed(Per5); // TrueConsole .log (Object.isfrozen (per5)); //"dwqs"; console.log (; //"pomy"per5.hi = function () {console.log ("hi" );}; console.log ("Hi" in per5); // falsedlete; console.log (; // "pomy" var desc = Object.getownpropertyDescriptor (Per5, "name") Download Admiral ;;; PERTOWNPROPERTYDESCRIPTOR (Per5, "Name") Console.log (desc.configurable); //falseconsole.log (Desc.writable); // false 
Note that three methods for modifying the object are only for objects The own attribute is valid, and the properties of the prototype object are invalid, and the properties can still be added or modified on the prototype.

Function Person (name) { = name;} var person1 = new Person (“pomy”); var person2 = new Person (“dwqs”); Object.freeze (Person1); person.prototype.hi = fUNCTION () {Console.log (“hi”);}; person1.hi (); // “hi”; person; person2.hi (); // “hi”;
Object.seal (OBJ): Seal object, at this time, the attribute of the OBJ becomes read-only, cannot be added, changed, or delete the property (all attributes are not configurable), The value of false is false,[[configurable]] value is false. You can use Object.Iseled (OBJ) to detect whether OBJ is seal.
This is easy to make people, generally, we are easy to understand as its value.

I hope the landlord is slightly modified, making people more easily understood.

I recommend it to understand

Object.Freeze frozen object, No changes in any properties and attribute values ​​cannot be performed.

Object.seal enclosed objects, compared to Object.Freeze, can modify the value of existing properties.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment