JavaScript Object

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

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";

Alternative syntax (using object literals):

Examples

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



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;
}

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");


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

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

grammar

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

Note: for /en. 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];
}