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

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

In addition, JavaScript allows custom objects.


Everything is an object

JavaScript provides several built-in objects, such as String, Date, Array, and so on. Only with special data type object properties and methods.

  • Boolean can be an object.
  • Numeric type can be an object.
  • String can also be an object
  • Date is an object
  • Mathematics and regular expressions are objects
  • An array is an object
  • Even the object function may be

JavaScript Object

Object is just a special kind of data. Objects have properties and methods.


Property access objects

Property is a value associated with an object.

Access object attribute syntax is:

objectName.propertyName

This example uses the length property of the String object to get the length of the string:

var message="Hello World!";
var x=message.length;

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

12


Object Access Method

The method is an operation that can be performed on the object.

You can call the method using the following syntax:

objectName.methodName()

This example uses the toUpperCase String object () method to convert the text to uppercase:

var message="Hello world!";
var x=message.toUpperCase();

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

HELLO WORLD!


Create a JavaScript object

Through JavaScript, you can define and create your own objects.

Create a new object in two different ways:

  • Define and create an instance of an object
  • Use functions to define the object, and then create a new object instance

Create a direct instance

This example creates a new instance of the object and add four properties:

Examples

person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";

try it"

Alternative syntax (using object literals):

Examples

person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};

try it"


Using the object constructor

This example uses the function to construct objects:

Examples

function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}

try it"

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)


Create a JavaScript object instance

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

var myFather=new person("John","Doe",50,"blue");
var myMother=new person("Sally","Rally",48,"green");


The properties to the JavaScript objects

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:

person.firstname="John";
person.lastname="Doe";
person.age=30;
person.eyecolor="blue";

x=person.firstname;

T in the above code is executed, the value of x will be:

John


The method to add objects to JavaScript

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.

Now you can try:

myMother.changeName("Doe");

try it"

JavaScript classes

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 ... in loop

JavaScript for ... in statement to loop through the object's properties.

grammar

for (variable in object)
{
	执行的代码……
}

Note: for ... in loop code block will be executed once for each property.

Examples

Loop through the properties of an object:

Examples

var person = {fname: "John", lname: "Doe", age: 25};

for (x in person)
{
txt = txt + person [x];
}

try it"