Home>

An object is actually a collection of data and functions.Objects can be created by executing the new operator followed by the name of the object type to be created.Create an object of type object and add properties and/or methods to it to create a custom object

Previous words

Object In javascript, the object is king;Almost everything in JavaScript is an object or works like an object.Understood the object,I understand javascript. In javascript, a reference type is a data structure,For organizing data and functionality,It is also often called a class.Reference types are sometimes called object definitions,Because they describe the properties and methods of a class of objects

Most reference values ​​are instances of the object type;Moreover, object is also the most used type in JavaScript.Although the object instance does not have much functionality,But for storing and transferring data in applications,They are indeed very ideal choices

Create object

There are two methods for creating object types

[1] object constructor

var person=new object ();
//If you don't pass parameters to the constructor, you can leave out the parentheses var person=new object;
person.name="bai";
person.age=29;
//Create empty object without 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 /));

note [Note] The object () constructor is itself an object,Constructors are objects created based on function constructors

[2] Using object literals

Javascript provides shortcuts called literals,Used to create most native object values.Using literals just hides the same as using the new operator

Basic process

var person={
name:"bai",age:29,5:true
};

note [Note] use commas in object literals to separate different attributes,But add a comma after the last attribute,Will cause error in ie7-

Use object literals to define objects,Property names are automatically converted to strings

//same as above
var person={
"name":"bai","age":29,"5":true
};

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

//equivalent to 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 pattern of passing parameters is most suitable for situations where a large number of optional parameters need to be passed to a function.Generally speaking,Although named parameters are easy to handle,But it is not flexible when there are multiple optional parameters.Therefore, using formal parameters for required values,And use object literals to encapsulate multiple optional parameters

Set object

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

advantage The two advantages of the bracket method are that the property can be accessed through variables, and the property name can be a JavaScript invalid identifier.

note [Note] Chinese can exist in the variable,Because Chinese is equivalent to characters,Treated the same as English characters,So it can be written as person.white or person ["White"]

var myobject={
123:"zero",class:"foo"
};
console.log (myobject ["123"], myobject ["class"]);//"zero" "foo"
console.log (myobject.123);//Error reported

If If the value in square brackets is non-string type, it will be converted to string implicitly using string () and output;If it is a string,If there are quotes, the original value is output,Otherwise it will be recognized as a variable,If the variable is undefined,Error

person [0]=1;//[] will not report an error,But automatically converted to a string
person [a]=1;//[] elements that meet the variable naming rules will be treated as variables,Variable is undefined,Error
person [""]=2;//[] will not report an empty string,Is actually present and can be called,But not in the collection on the right side of the console
person [undefined or null or true or false]=4;//no error will be reported,But automatically converted to a string
person ["White"]=6;//no error

Delete object

The delete operator can be used to completely delete an attribute from an object.delete is the only way to delete attributes from an object,Setting a property to undefined or null can only change the value of the property,It does not delete the attribute from the object.delete can only delete the data under the object,The values ​​of the other 5 basic types cannot be deleted

[Note] delete does 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 the variable a is declared in the global state, it is equivalent to a data a under the window object. You can assign a to a through window.a or a, and the values ​​of window.a and a are always equal.But just can't delete

var a;
a=10;
console.log (a, window.a);//10 10
window.a=20;
console.log (a, window.a);//20 20
delete a;
console.log (a, window.a);//20 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 being declared under the window object), you can delete it.And delete b and delete window.b have the same effect,After deleting, console.log (b) prompts that the variable does not exist.console.log (window.b) prompts undefined

window.b=10;
console.log (b, window.b);//10 10
delete b;
console.log (b);//Error reported
console.log (window.b);//undefined
window.b=10;
console.log (b, window.b);//10 10
delete window.b;
console.log (b);//Error reported
console.log (window.b);//undefined

Object nesting

Objects can be nested,But must take values ​​layer by layer

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

note [Note] The value can only be taken layer by layer,Such as student.name.chinese, but ca n’t cross the name, use student.chinese directly, because there may be an element called chinese under the same level as 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:holds the function used to create the current object

Hasownproperty (propertyname):Used to check if a given property exists in the current object instance (not in the instance's prototype).Among them, propertyname must be specified as a string

Isprototypeof (object):used to check whether the passed-in object is the prototype of the passed-in object

Propertyisenumerable (propertyname):Used to check whether a given property can be enumerated using a for-in statement.Among them, propertyname must be specified as a string

Tolocalestring ():Returns a string representation of the object,This string corresponds to the region of the execution environment

Stringtostring ():Returns a string representation of the object

Valueof ():Returns a string, numeric, or Boolean representation of the 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 functions.Objects can be created by executing the new operator followed by the name of the object type to be created.By creating an instance of the object type and adding properties and/or methods to it, you can create a custom object.

var o=new object ();

Examples Each instance of object has the following properties and methods:

● constructor-holds the function used to create the current object

● hasownproperty (propertyname)-used to check if a given property exists in the current object instance (not in the instance's prototype).Among them, the property name (propertyname) as a parameter must be specified as a string (for example:o.hasownproperty ("name"))

● isprototypeof (object)-used to check whether the passed object is a prototype of another object

Propertyisenumerable (propertyname)-used to check whether a given property can be enumerated using a for-in statement

● tostring ()-returns a string representation of the object

● valueof ()-returns a string, numeric, or Boolean representation of the object.Usually the same as the return value of the tostring () method.

  • Previous Raw and complex values ​​in JavaScript
  • Next Java implements HTTP Post, Get, proxy access requests