Home  >  Article  >  Web Front-end  >  Detailed explanation of javascript functions, properties, and object creation expressions

Detailed explanation of javascript functions, properties, and object creation expressions

伊谢尔伦
伊谢尔伦Original
2017-07-19 10:02:231758browse

Function expression

Function definition expression defines a javascript function. The value of the expression is this newly defined function. In a sense, function definition expressions can become function literals, and function expressions can be called "function literals." After all, object initialization expressions are also called "object literals." A typical function definition expression contains the keyword function, followed by a pair of parentheses. Within the parentheses is a comma-separated list containing 0 or more identifiers (parameter names). Then follow the JavaScript code segment (function body) wrapped in curly braces.

var square = function(x){ return x*x};

Attribute access expression

The attribute access expression operation obtains the value of an object or an array element. JavaScript defines two methods for property access.

expression . indentifier
expression [expression]

The first way of writing is an expression followed by a period and an identifier. The expression specifies the object, and the identifier specifies the property to be accessed.
The way to write Chapter 2 is to use square brackets, and inside the square brackets is an expression (this method is suitable for objects and arrays). The second expression specifies the index of the property to be accessed or represents the index of the array element to be accessed. Here are some specific examples

o.x //=>1表达式o的x属性
o.y.z //=>3 表达式o.y的z属性
o.["x"] //=>1的对象o的x属性
a[1]  //=>4 表达式a索引为1的元素
a[2]["1"]//=>6 表达式a[2]中索引为1的元素
a[0].x //=>1: 表达式a[0]的x属性

No matter which form of attribute access expression is used, the expression before "." and "[" will always be evaluated first. If it evaluates to null or undefined, the expression will throw a TypeError exception because neither value can contain any properties. If the result of the operation is not an object or array, JavaScript will convert it into an object (Chapter 3, Section 6)

Although the .identifier way of writing is simpler, it should be noted that this method is only suitable for accessing The attribute names are legal identifiers. And need to know the name of the property to be accessed. If the property name is a reserved word or contains spaces and punctuation, or is a number (for arrays), it must be written in square brackets. When the attribute name is a value obtained by an operator rather than a fixed value, square brackets must be used.

Deployment expression

The invocation expression (invocation expression) in JavaScript is a grammatical representation of calling (or executing) a function or method. It starts with a function expression that refers to the function to be called. A function expression is followed by a pair of parentheses containing a comma-separated list of parameters. There can be 0 or more parameters.

f(0) //f is a function expression: 0 is a parameter expression.
Math.max(x,y,z) //Math.max is a function; x, y and z are parameters
a.sort() //a.sort() is a function, it does not parameter.
When an expression is called for evaluation, the function expression is first calculated, and then the parameter expression is calculated to obtain a set of parameter values. If the value of the function expression is not a callable object, a type error exception is thrown. Then the values ​​of the parameters are assigned to the formal parameters in turn, which are defined when the function is defined. Next execute the function body. If the function uses the return statement to give a return value, then the return value is the value of the entire calling expression. Otherwise, the value of the calling expression is undefined. Details of function calls—including what happens when the number of formal parameter expressions does not match the number of actual parameters in the function definition—will be explained in detail in Chapter 8. .

Any call expression contains a pair of parentheses and the expression before the left parenthesis. If the expression is a property access expression, then the call is called a "method invocation". When the function body is executed in a method call, the objects and arrays used as attributes to access the body are the points pointed to by this in the calling method. This feature allows a function (whose OO name is "method") to call its host object in the paradigm of object-oriented programming

Object creation expression

Object creation expression expression) creates an object and calls a function (constructor) to initialize the object's properties. The object creation expression is very similar to the function call expression, except that there is an additional keyword new:

new Object()
new Point(2,3)


before the object creation expression.

The above is the detailed content of Detailed explanation of javascript functions, properties, and object creation expressions. For more information, please follow other related articles on the PHP Chinese website!

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