The best JavaScript Object Tutorial In 2024, In this tutorial you can learn Everything is an object,JavaScript Object,Property access objects,Object Access Method,Create a JavaScript object,Create a direct instance,Examples,Examples,Using the object constructor,Examples,Create a JavaScript object instance,The properties to the JavaScript objects,The method to add objects to JavaScript,Now you can try:,JavaScript classes,JavaScript for ... in loop,Examples,
All things are JavaScript objects: strings, numbers, arrays, functions /en.
In addition, JavaScript allows custom objects.
JavaScript provides several built-in objects, such as String, Date, Array, and so on. Only with special data type object properties and methods.
Object is just a special kind of data. Objects have properties and methods.
Property is a value associated with an object.
Access object attribute syntax is:
This example uses the length property of the String object to get the length of the string:
After the above code is executed, the value of x will be:
The method is an operation that can be performed on the object.
You can call the method using the following syntax:
This example uses the toUpperCase String object () method to convert the text to uppercase:
After the above code is executed, the value of x will be:
Through JavaScript, you can define and create your own objects.
Create a new object in two different ways:
This example creates a new instance of the object and add four properties:
Alternative syntax (using object literals):
This example uses the function to construct objects:
In JavaScript, this usually points to a function that we are executing itself, or an object that points to the function belongs to (run-time)
Once you have the object constructor, you can create a new instance of the object, like this:
You can assign an object, add new attributes to existing objects:
Suppose personObj already exists - which you can add these new attributes: firstname, lastname, age and eyecolor:
T in the above code is executed, the value of x will be:
The method is simply attached to the object's function.
In the method of the constructor function inside the definition of an object:
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.changeName=changeName; function changeName(name) { this.lastname=name; } }
() Value of the function name of the person assigned to changeName lastname properties.
JavaScript is an object-oriented language, but JavaScript does not use class.
In JavaScript, the class is not created, nor to create an object (as in the other object-oriented languages) through the class.
JavaScript-based prototype, rather than class-based.
JavaScript for /en. in statement to loop through the object's properties.
for (variable in object) { 执行的代码…… }
Note: for /en. in loop code block will be executed once for each property.
Loop through the properties of an object: