Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript Object

JavaScript object data have attributes and methods.


Real-life objects, properties, and methods

In real life, a car is an object.

Object has its attributes, such as weight and color, start stop, etc. methods:

Objects Attributes method

car.name = Fiat

car.model = 500

car.weight = 850kg

car.color = white

car.start ()

car.drive ()

car.brake ()

car.stop ()

All cars have these properties, but the properties of each car is different.

All cars have these methods, but the time they are executed are different.


JavaScript Object

In JavaScript, almost all things are objects.

Note In JavaScript, objects are very important when you understand the object, you can learn JavaScript.

You have learned assignment JavaScript variables.

The following code for the variable car set the value "Fiat":

var car = "Fiat";

The object is a variable, but the object can contain multiple values ​​(multiple variables).

var car = {type: "Fiat ", model: 500, color: "white"};

In the above example, the three values ​​( "Fiat", 500, "white") to the variable car.

In the above example, the three variables (type, model, color) to the variable car.

Note JavaScript object is a container of variables.


Object definition

You can use a character to define and create a JavaScript object:

Examples

var person = {firstName: "John ", lastName: "Doe", age: 50, eyeColor: "blue"};

try it"

Custom JavaScript objects can span multiple lines, spaces with newline is not required:

Examples

var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};

try it"


Object Properties

You can say "JavaScript object is a container of variables."

However, we usually think "JavaScript object is the key to the container."

Key-value pair is usually written as name: value (with key values separated by colons).

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

Note JavaScript object is a container attribute variables.

Object key to the wording similar to:

  • Associative arrays in PHP
  • Python in dictionary
  • C language hash table
  • Java hash map
  • Ruby and Perl hash table

Access object properties

You can access object properties in two ways:

Example 1

person.lastName;

try it"

Example 2

person [ "lastName"];

try it"


Object Methods

The method defines a function object, and stored as an attribute of the object.

Object method by adding () call (as a function).

The examples of the person accessing the object fullName () method:

Examples

name = person.fullName ();

try it"

If you want to access the object person fullName property, which is defined as a string returned by the function:

Examples

name = person.fullName;

try it"

Note JavaScript object is a container properties and methods.

In the following tutorial you will learn more about the functions, properties, and methods of knowledge.


Object Access Method

You can use the following syntax to create object methods:

methodName: function () {code lines}

You can use the following syntax to access the object methods:

objectName.methodName ()

Typically fullName () method as a person object, fullName as an attribute.

There are several ways to create, use, and modify the JavaScript objects.

There are also a variety of ways to create, use, and modify the properties and methods.

Note In the following tutorial, you will learn more about the object of knowledge.


More examples

I create JavaScript objects

Create a JavaScript object II

Access object properties I

Access Object Properties II

Function attribute as a way to access

Function attribute as an attribute access