Home >Web Front-end >JS Tutorial >Javascript Basic Tutorial: Function Objects and Properties_Basic Knowledge

Javascript Basic Tutorial: Function Objects and Properties_Basic Knowledge

WBOY
WBOYOriginal
2016-05-16 16:19:401150browse

The data contained in the object can be accessed in two ways

Both property and method

Attributes are variables belonging to a specific object, and methods are functions that can only be deployed by a specific object.

An object is a data entity that is composed of some related properties and methods. In JavaScript, properties and methods are accessed using "dot" syntax.

Copy code The code is as follows:

Object.proprty
Object.method()

Suppose a car has brand, band, and color attributes. You can access these attributes through the following methods

Copy code The code is as follows:

Car.band
Car.color

Assume that Car is associated with some functions such as move(), stop(), and addOil. These functions are the methods of the Car object. You can use the following methods to dispatch it

Copy code The code is as follows:

Car.move()
Car.stop()
Car.addOil()

These properties and methods are gathered together to form the Car object. In other words, the Car object can be regarded as the collective name of all these properties and methods.

In order for the Car object to describe a specific car, you need to create an instance of the Car object. The instance is the specific representation of the object. Objects are collective names and instances are individuals.

For example, BMW and Xiali are all cars, and they can all be described as Car. A BMW and a Xiali are both different sizes. They are both Car objects, but different instances.

In JavaScript, create a new instance using the new keyword. As follows

var myCar = new Car();
The above code creates a new instance of the Car object myCar. With this instance, you can use the properties and methods of the Car object to retrieve the properties and methods of myCar. The code is as follows

Copy code The code is as follows:

myCar.band
myCar.addOil()

In JavaScript, strings and arrays are objects. Strictly speaking, everything is an object

Copy code The code is as follows:

var aValues ​​= new Array();
var myString = new String("hello world")

Use Date object to test the execution speed of the computer

Copy code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn