Javascript uses function to define constructors_javascript tips
The syntax for creating objects in Javascript is the new operator followed by a function call. For example,
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.
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:
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
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
var object=new objectname();
var -- declare object variable
object -- the name of the object
new -- new keyword (JavaScript keyword)
objectname -- Constructor name
Example
//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
//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
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

In Python, every class has a constructor, which is a special method specified inside the class. The constructor/initializer will be called automatically when a new object is created for the class. When an object is initialized, the constructor assigns values to data members in the class. There is no need to define the constructor explicitly. But in order to create a constructor, we need to follow the following rules - For a class, it is allowed to have only one constructor. The constructor name must be __init__. Constructors must be defined using instance properties (just specify the self keyword as the first argument). It cannot return any value except None. Syntax classA():def__init__(self):pass Example Consider the following example and

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

The usage of js function function is: 1. Declare function; 2. Call function; 3. Function parameters; 4. Function return value; 5. Anonymous function; 6. Function as parameter; 7. Function scope; 8. Recursive function.

C++ is a widely used object-oriented programming language. When defining the constructor of a class in C++, if you want to place the definition of the constructor outside the class, you need to add the class name as a qualifier to the definition of the constructor. To specify which class this constructor belongs to. This is a basic rule of C++ syntax. If this rule is not followed when defining the constructor of a class, a compilation error will appear, prompting "Constructors defined outside the class must be qualified with the class name." So, if you encounter this kind of compilation error, you should

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file
