Home  >  Article  >  Web Front-end  >  JavaScript Tutorial Custom Object

JavaScript Tutorial Custom Object

黄舟
黄舟Original
2016-12-22 15:40:421334browse

Create your own objects using JavaScript. Although JavaScript and the browser itself are very powerful, JavaScript still provides methods for creating a new object. This makes it possible to complete many complex tasks without requiring the help of hypertext markup languages ​​or other multimedia tools.

Creating a new object in JavaScript is very simple. First it must define an object, and then create an instance of the object. This instance is a new object that has the basic characteristics of the object definition.

Definition of object

Definition of JavaScript object, its basic format is as follows:

Function Object (property table)

This.prop1=prop1

This.prop2=prop2

...

This.meth= FunctionName1;

This.meth=FunctionName2;

...

In the definition of an object, you can specify its properties and methods for the object. An instance of an object is composed of properties and methods. For example, the following is a definition of the University object:

Function university(name,city,creatDate URL)

This.name=name

This.city=city

This.creatDate=New Date(creatDate)

This .URL=URL

The basic meaning is as follows:

Name - Specify a "unit" name.

City - the city where the "unit" is located.

CreatDate - records the update date of the university object.

URL – This object points to a URL.


Create an object instance

Once the object definition is completed, you can create an instance of the object:

NewObject=New object();

where Newobjet is a new object and Object has been defined . Example:

U = New university("Jiangsu Province", "Nanjing City", "January 05,2014 12:00:00", "http://www.51ebk.com")


Object method The use of

In addition to using attributes in objects, sometimes you also need to use methods. In the definition of the object, we see the This.meth=FunctionName statement, which is the method for defining the object. The method of the actual object is a function FunctionName, through which it realizes its intention.

The example adds a method to the university object, which displays itself and returns the corresponding string.

function university(name,city,createDate,URL)

This.Name=Name;

This.city=city;

This.createDate=New Date(creatDate);

This.URL=URL;

This.showuniversity=showuniversity;

This.showuniversity defines a method--showuniversity(). The showuniversity() method implements the display of the university object itself.

function showuniversity()

For (var prop in this)

alert(prop+="+this[prop]+"");

where alert is an internal function in JavaScript that displays its string.

The above is the content of the JavaScript tutorial on custom objects. For more related content, please pay attention to the PHP Chinese website (www.php.cn)


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