Home  >  Article  >  Web Front-end  >  Javascript uses function to define constructors_javascript tips

Javascript uses function to define constructors_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:33:451213browse

The syntax for creating objects in Javascript is the new operator followed by a function call. For example,

Copy code The code is as follows:

var obj = new Object();
var date = new Date();

Operator new first creates a new object without any attributes, and then calls the function, passing the new object as the value of the this keyword.
Copy code The code is as follows:

Pseudocode implementation of var date = new Date() That is
var obj = {};
var date = Date.call(obj);

The function of the constructor is to initialize a newly created object and set it before using the object properties of the object. If you define your own constructor, you only need to write a function that adds attributes to this. The following code defines a constructor:
Copy code The code is as follows:

function Rectangle( w, h)
{
this.width = w;
this.height = h;
}

Then, you can use the new operator to call this function Create an instance of the object
Copy the code The code is as follows:

var rect = new Rectange(4, 8);

Constructor return value
Constructors in Javascript usually have no return value. However, functions are allowed to return values. If a constructor has a return value, the returned object becomes the value of the new expression. In this case, the object used as this will be discarded.

Use constructor definition method
Syntax
Copy code The code is as follows:

var object=new objectname();
var -- declare object variable
object -- the name of the object
new -- new keyword (JavaScript keyword)
objectname -- Constructor name

Example
Copy code The code is as follows:

//Define constructor
function Site(url, name)
{
this.url = "www.jb51.net";
this.name ="Dream City";
}
//Use the constructor to generate an instance of a JavaScript object
var mysite = new Site();
alert(mysite.url);

The constructor can usually Initialize some contents in the object. Some objects provided within JavaScript usually need to be generated using the constructor method. The content of JavaScript functions will be introduced in the next chapter.

Create JavaScript objects by direct definition
Copy code The code is as follows:

//Define object syntax
var object={};
//Attribute syntax within the object (property name (property) and attribute value (value) appear in pairs)
object.property=value ;
//Function syntax within the object (function name (func) and function content appear in pairs)
object.func=function(){...;};

var -- declare object variable
object -- the name of the object
property -- the property name of the object
func -- the method name of the object
Description: Objects can contain some properties (functions can Seen as special properties with parentheses), each property has a name and a value. The name can be any string or even empty. The value can be any JavaScript type, but cannot be undefined.

Example of object defined using definition method
Copy code The code is as follows:

var site = {};
site.URL = "www.jb51.net";
site.name = "Script Home";
site.englishname = "jb51";
site .author = "Script";
site.summary = "Free Web Design Tutorial";
site.pagescount = 100;
site.isOK = true;
site.startdate = new Date( 2005, 12);
site.say = function(){alert(this.englishname " say : hello world!")};
site.age = function(){var theage=(new Date() .getFullYear())-site.startdate.getFullYear();alert(this.name "already" theage "year old!")}

Example of using a constructor to create a JavaScript object--you can try editing
Using a constructor to create a JavaScript object
The above method defines a site object and defines seven properties for it, as well as two personal properties. method.

The say method will print out the string of jb51 say: hello world!
The age method will calculate the age of the Dream City website
Monkey Tip: Note that each attribute and function must be preceded by The name of the object, otherwise JavaScript cannot determine which object it belongs to.

The following course will explain the extension of the direct definition method, the JSON definition method.
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