How do I understand JavaScript objects?

Source: Internet
Author: User

In our lives, we often refer to the Word object, such as: Have you found the object? Who's your target? Wait a minute.

In our family, a young woman with a boyfriend will say I have a target, then her object is xx (her boyfriend).

What about the couple? Will say that my lover is who who, now we should know what is the object of it.

There is a proverb: All things are objects, in the eyes of our developers, the program is to reflect the real life. So how do we write scripts in JavaScript to feed back the needs of life information?

Defining multiple properties and functions in our JavaScript functions is a JavaScript object.

Definition in concept: Object-oriented (Object-oriented,oo) languages have a sign that they all have the concept of a class.

But there is no concept of class in JavaScript, only the concept of a function, and we typically use a function to simulate a class.

In life our object-oriented is made up of attributes and behaviors. such as: Your Sex-male (this is the attribute), I am working (this is the behavior).

In the program we can create multiple properties and functions arbitrarily through the class.

In ECMA-262, the object is defined as: "A collection of unordered properties whose properties can contain basic values, objects, or functions."

Let me take a look at the example below! See how to create objects, how to Access object properties and Object functions (methods; function terms in Java).

Object definition:

All things in JavaScript are objects: strings, numbers, arrays, functions ...

In addition, JavaScript allows custom objects

All things are objects

JavaScript provides multiple built-in objects, such as String, Date, array, and so on. Objects are just special data types with properties and methods.

    • The Boolean type can be an object
    • A digital type can be an object
    • A string can be an object
    • Date is an object
    • Math and regular expressions are also objects
    • An array is an object
    • Even a function can be an object

var cars = {

Name: "Flat",

MODEL:500,

weight:850kg,

Color:white

}

The variable is the container for the object of cars

Object properties:

You can say "JavaScript objects are containers for key-value pairs."

However, we generally think that "JavaScript objects are containers for key-value pairs."

Key-value pairs are usually written as name:value (the keys and values are separated by colons).

Key-value pairs are commonly referred to as object properties in JavaScript objects.

To access object properties:

You can access object properties in two ways:

Example 1:

Person.lastname;

Example 2:

person["LastName"];

Object method:

The method of the object defines a function and stores it as an object's property.

The object method is called (as a function) by adding ().

The instance accesses the FullName () method of the Person object:

Instance:

Name = Person.fullname ();

If you want to access the FullName property of the person object, it is returned as a string that defines the function:

name = Person.fullname;

JavaScript objects are containers for properties and methods.

To access the object method:

You can create an object method using the following syntax:

Methodname:function () {Code lines}

You can access the object methods using the following syntax:

Objectname.methodname ()

Usually FullName () is a method that acts as a person object, and FullName is a property.

There are several ways to create, use, and modify properties and methods.

Using the Object Builder:

The constructor means: create something, such as: properties, functions, and so on.

This example uses a function to construct an object:

Instance

function Person (firstname,lastname,age,eyecolor) {

This.firstname=firstname;

This.lastname=lastname;

This.age=age;

This.eyecolor=eyecolor;

}

In JavaScript, this usually points to the function that we are executing, or to the object that the function belongs to (runtime).

To create an instance of a JavaScript object:

Once you have the object constructor, you can create a new object instance, just like this:

var myfather = new Person ("John", "Doe", "Blue");

var mymother = new Person ("Sally", "Rally", and "green");

Add attributes to JavaScript objects

You can add properties to an existing object by assigning a value to the object:

Assuming personobj already exists, you can add these new attributes to it: FirstName, LastName, age, and Eyecolor;

Person.firstname= "John";

Person.lastname= "Doe";

person.age=30;

Person.eyecolor= "Blue";

X=person.firstname;

After the above code is executed, the value of X will be:

John

Add a method to a JavaScript object

method is simply a function attached to an object.

Methods for defining objects inside the constructor function:

function person (firstname,lastname,eyecolor)

{

This.firstname=firstname;

This.lastname=lastname;

This.age=age;

This.eyecolor=eyecolor;

This.changename=changename;

function ChangeName (name)

{

This.lastname=name;

}

}

The value of the ChangeName () function name is assigned to the LastName property of the person.

Mymother.changename ("Doe");

JavaScript class

JavaScript is an object-oriented language, but JavaScript does not use classes.

In JavaScript, classes are not created and objects are not created through classes (as in other object-oriented languages).

JavaScript is based on prototype (prototypes), not class-based.

How do I understand JavaScript objects?

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.