Home > Web Front-end > JS Tutorial > What is a function in javascript

What is a function in javascript

青灯夜游
Release: 2023-01-07 11:44:15
Original
3486 people have browsed it

In JavaScript, a function (Function) is a piece of encapsulated code that can be used (called) repeatedly; a function can be a value, an object, a type of data, or an expression. Therefore, functions can assign values, perform operations, have properties and methods, and can even temporarily store values, construct instances, etc.

What is a function in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, object is a general concept. Any value can be converted into an object and used in the form of objects, such as numerical objects, Boolean objects, string objects, type objects, and function objects. , array objects, etc., they all inherit Object type objects and have common basic properties and methods. In addition, JavaScript also allows custom objects.

Analyzed from a narrow concept, object (Object) is the most basic data type. It is a composite structure and reference data. It is an unordered data collection. Each member of the object is called Attributes.

JavaScript is an object-based, but not entirely object-oriented programming language. In the JS object-oriented programming model, there are two core concepts: objects and classes. Before the ECMAScript6 specification, JavaScript had no concept of classes and only allowed classes to be simulated through constructors and inheritance through prototypes.

JS Create Object

Construct Object

Use the new operator to call the constructor, you can Construct an instance object. The specific usage is as follows:

var objectName = new functionName(args);
Copy after login

Parameter description is as follows:

  • objectName: the returned instance object.

  • functionName: Constructor, basically the same as an ordinary function, but does not need to return a return value and returns an instance object, which can be accessed in advance using this within the function.

  • args: Instance object initialization configuration parameter list.

Example

The following example uses different types of constructors to define various instances.

var o = new Object();  //定义一个空对象
var a = new Array();  //定义一个空数组
var f = new Function();  //定义一个空函数
Copy after login

Object direct quantity

Using direct quantities can quickly create objects, and it is also the most efficient and simplest method. The specific usage is as follows:

var objectName = {
    属性名1 : 属性值1,
    属性名2 : 属性值2,
    ...
    属性名n : 属性值n
};
Copy after login

In object literals, the attribute name and attribute value are separated by colons. The attribute value can be any type of data, and the attribute name can be a JavaScript identifier or a string. type expression. Properties are separated by commas, and no comma is required at the end of the last property.

Example

The following code uses object literals to define two objects.

var o = {  //对象直接量
    a : 1,  //定义属性
    b : true  //定义属性
}
var o1 = {  //对象直接量
    "a" : 1,  //定义属性
    "b" : true  //定义属性
}
Copy after login

JavaScript constructor

JavaScript constructor (Constructor) is also called a constructor and a type function. It functions like an object template, a constructor A function can generate any number of instances. The instance objects have the same attributes and behavioral characteristics, but are not equal.

Define the constructor

There is no difference between the constructor and the ordinary function in terms of syntax and usage. The method of defining a constructor is as follows:

function 类型名称 (配置参数) {
    this.属性1 = 属性值1;
    this.属性2 = 属性值2;
    ...
    this.方法1 = function () {
        //处理代码
    };
    ...
    //其他代码,可以包含return语句
}
Copy after login

It is recommended that the first letter of the name of the constructor be capitalized to distinguish it from ordinary functions.

The constructor has two distinctive features.

  • Use this in the function body to refer to the instance object to be generated.

  • You must use the new command to call the function to generate an instance object.

Example

The following example demonstrates defining a constructor that contains two properties and a method.

function Point (x, y) {  //构造函数
    this.x = x;  //私有属性
    this.y = y;  //私有属性
    this.sum = function () {  //方法
        return this.x + this.y;
    }
}
在上面代
Copy after login

In the code, Point is the constructor, which provides a template to generate instance objects.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is a function in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template