The JavaScript type System's object detailed _javascript skill

Source: Internet
Author: User
Tags instance method object object hasownproperty

Front.

In JavaScript, objects are king, and almost everything in JavaScript is object or object-like. When you understand the object, you understand JavaScript. In JavaScript, a reference type is a data structure that is used to organize data and functionality, and is often referred to as a class. Reference types are sometimes referred to as object definitions because they describe the properties and methods of a class of objects

The values for most reference types are instances of type object, and object is the most commonly used type in JavaScript. Although the object instance does not have much functionality, it is really an ideal choice for storing and transferring data in an application

Creating objects

There are two types of object creation methods

[1] Object constructor

var person = new Object ();
If you do not pass arguments to the constructor, you can not bracket the var person = new Object;
Person.name = ' Bai ';
Person.age =; 
Create an empty object with no attributes
var cody1 = new Object ();
var cody2 = new Object (undefined);
var cody3 = new Object (null);
Console.log (typeof cody1,typeof Cody2, typeof cody3);//object Object Object 
//create string, number, array, function, Boolean, Regex
Console.log (New Object (' foo '));
Console.log (New Object (1));
Console.log (New Object ([]));
Console.log (New Object (function () {}));
Console.log (New Object (true));
Console.log (New Object (/\bbt[a-z]+\b/));

Attention The object () constructor itself is an objects that are created based on the function constructor

[2] using the literal amount of the object

JavaScript provides shortcuts, called literal quantities, to create most native object values. Use literal only hides the same as using the new operator

Basic process

var person = {
name: ' Bai ',
age:29,
5:true
};

[note] Use commas in object literals to separate different properties, but adding commas after the last attribute causes an error in ie7-

Object literals are defined by using object literal methods, and property names are automatically converted to strings

Ibid.
var person = {
' name ': ' Bai ',
' age ': '
5 ': true
};

If you leave the curly braces blank, you can define objects that contain only the default properties and methods

Equivalent to the var person = new Object ();
var person = {}; 
[Tips] Use object literals to encapsulate multiple optional parameters
function DisplayInfo (args) {
var output = ';
if (typeof args.name = = ' string ') {
output = = ' Name: ' + args.name + ' \ n ';
}
if (typeof args.age = = ' number ') {
output + = ' Age: ' + args.age + ' \ n ';
}
Console.log (output);
DisplayInfo ({
name: ' Nicholas ',
age:29
});
DisplayInfo ({
name: ' Match '
});

The above mode of passing parameters is best suited for situations where you need to pass in a large number of optional parameters to a function. In general, although named parameters are easy to handle, there are multiple optional parameters that are inflexible. Therefore, you use formal parameters for the required values, and you use object literals to encapsulate multiple optional parameters

Set Object

There are two ways to access object properties, and you can get, set, or update the properties of an object using dot notation or the bracket notation

The two advantages of the Bracket method are that the property can be accessed through a variable, the name of the property can be a JavaScript invalid identifier

[note] There can be Chinese in a variable because Chinese is the equivalent of a character and is treated as a literal, so it can be written as a person. White or person[' white '

var myObject = {
123: ' Zero ',
class: ' foo '
};
Console.log (myobject[' 123 '],myobject[' class ');/' zero ' foo '
console.log (myobject.123);/error

The value in square brackets, if not a string type, is implicitly converted to string output using string () and, if it is a string type, is recognized as a variable if it is in quotation marks, or if the variable is undefined, the error

Person[0] = 1; [] The number in the [] is automatically converted to a string
person[a] = 1;//[] The element that conforms to the variable naming rule is treated as a variable, the variable is not defined, and
the error person["= 2;//[] The empty string does not complain. is actually present and can be invoked, but will not display person[undefined or null or TRUE or FALSE in the collection on the right side of the console
= 4;//does not error, but is automatically converted to a string

Delete Object

The delete operator can be used to completely remove a property from an object. Delete is the only way to remove a property from an object, and setting the property to undefined or null can only change the value of the property without removing the property from the object. Delete Deletes only the data under the object, and the values of the other 5 underlying types are not deleted

[Note that]delete will not delete attributes found on the prototype chain]

var foo = {bar: ' Bar '};
Delete Foo.bar;
Console.log (' Bar ' in foo);//false 
var a = 123;
Delete A;
Console.log (a);//123

If you declare variable a globally, the equivalent of a data a in the Window object, you can assign a value to a by WINDOW.A or a, and the value of WINDOW.A and a is always equal, but you cannot delete

var A;
A = ten;
Console.log (A,WINDOW.A);//10
window.a =;
Console.log (A,WINDOW.A);//20
Delete A;
Console.log (A,WINDOW.A);//20
delete window.a;
Console.log (A,WINDOW.A);//20 20

If you use WINDOW.B to declare and assign a value (b is equivalent to declaring under a Window object), you can delete it and use the same effect as delete B and delete window.b, after deletion, Console.log (b) Prompts the variable does not exist, Console.log ( WINDOW.B) hint undefined

window.b = ten;
Console.log (b,window.b);//10
Delete b;
Console.log (b);//Error
Console.log (window.b);//undefined 
window.b = ten;
Console.log (b,window.b);//10
delete window.b;
Console.log (b);/error

Object nesting

objects can be nested, but must be evaluated at a level

var student = {
name: {
chinese:1,
englisth:2
},
sex:1,
age:26
}

[note] Values can be taken only one layer, such as Student.name.chinese, but not across name, directly with Student.chinese, because there may also be an element called Chinese under the sibling of name

var object1 = {
object1_1:{
object1_1_1:{foo: ' Bar '},
object1_1_2:{}
},
object1_2:{
object1_2_1:{},
object1_2_2:{}
}
;
Console.log (Object1.object1_1.object1_1_1.foo);//bar

Instance method

Constructor: Save the function used to create the current object
hasOwnProperty (PropertyName): Checks whether a given property exists in the current object instance (not in the prototype of the instance). Where the PropertyName must be specified as a string

isPrototypeOf (object): Used to check if an incoming object is a prototype of an incoming object

propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated using the For-in statement. Where the PropertyName must be specified as a string

toLocaleString (): Returns a string representation of an object that corresponds to the locale of the execution environment

ToString (): Returns the string representation of an object

valueof (): Returns the string, numeric, or Boolean value of an object, usually the same as the return value of the ToString () method

var myObject = {
mark:true
};
Console.log (myobject.constructor);//function Object () {}
Console.log (Myobject.hasownproperty (' mark '));//true
Console.log (Object.prototype.isPrototypeOf (MyObject));//true
Console.log ( Myobject.propertyisenumerable (' mark '));//true
Console.log (myobject.tolocalestring ());//[object Object]
Console.log (Myobject.tostring ());//[object Object]
Console.log (typeof myobject.valueof (), myobject.valueof ()) ;//Object Object{mark:true}

Summary:

Object type

An object is actually a collection of data and functionality. An object can be created by executing the new operator followed by the name of the object type to be created. You can create a custom object by creating an instance of type object and adding properties and/or methods to it.

 
 

Each instance of object has the following properties and methods:

constructor--saves the function used to create the current object
hasOwnProperty (PropertyName)--for checking whether a given property exists in the current object instance (not in the prototype of the instance). Where the property name (PropertyName) as a parameter must be specified as a string (for example: O.hasownproperty ("name"))
isPrototypeOf (object)--Used to check if an incoming object is a prototype of another object
propertyIsEnumerable (PropertyName)--Used to check whether a given property can enumerate with for-in statements
ToString ()--Returns the string representation of an object
valueof ()--Returns a string, numeric, or Boolean representation of an object. Usually the same as the return value of the ToString () method.

Related Article

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.