Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of JavaScript object, object property and object method

Shulou Source: shulou.com Published: 2022-06-01 10:28:39 09月25日 Update

This article mainly introduces the JavaScript object, object properties, object method example analysis, the article introduces in great detail, has a certain reference value, interested friends must read it!

The JavaScript variable is a container for data values.

This code assigns a single value (porsche) to a variable named car:

Var car = "porsche"

Objects are also variables. But the object contains many values.

This code assigns multiple values (porsche, 911, white) to a variable named car:

Var car = {type: "porsche", model: "911", color: "white"}

Values are written as name: value pairs (names and values are separated by colons).

The JavaScript object is a container for named values.

Object attribute

(in the JavaScript object) name: value pairs are called properties.

Var person = {firstName: "Bill", lastName: "Gates", age:62, eyeColor: "blue"}

Attribute value

FirstName Bill

LastName Gates

Age 62

EyeColor blue

Object method

Objects can also have methods.

Method is an action performed on an object.

Method is stored in the property as a function definition.

Attribute value

FirstName Bill

LastName Gates

Age 62

EyeColor blue

FullName function () {return this.firstName + "" + this.lastName;}

Method is a function that is stored as an attribute.

Example

Var person = {

FirstName: "Bill"

LastName: "Gates"

Id: 678

FullName: function () {

Return this.firstName + "" + this.lastName

}

}

These are all the contents of the article "sample Analysis of JavaScript objects, object Properties, and object methods". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Objects properties methods variables names examples analysis code content functions containers articles storage values interests colons actions multiple instances guys Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Redmi Xiaomi Shulou Tech Info