JavaScript Small details Dot list

Source: Internet
Author: User

As we all know, JavaScript defines two syntax methods for access to properties:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

expression. Identifier//expression (specified object) identifier (specifies the name of the property that needs to be accessed)
Expression 1 (Specified object) var O = {A:1,b:{c:3}}var a = [o,4,[5,6,7]]o.a//1o.b.c//3o["a"]//1a[1]//4A[0].B.C//3a[2]["2"]//7

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The first way we use the dot "." For property access, the second is to use square brackets for property access. In fact, as we can easily find the difference between the two methods, the second method in square brackets is also an expression.

But the first expression of both methods is evaluated first, and if the result is null or undefined then an error is thrown, and if the result of the operation is not an object or an array, JS converts it into an object (such as a string of code blocks below).

var str = "Hello world!"; var world = s.substring (S.indexof ("") +1, S.length)

If the result of the operation is not an object or an array, JS will convert it to Object = " Some people will wonder if the string is not an object, why does it have properties?" This is because when we do the property access, JS will call the string new String (str) to convert to an object, and provide this method of the newly created object will inherit (about the object's inheritance can consult the relevant data) string method and used to handle the property reference. Once the attribute reference is finished, the newly created object is destroyed. Of course, I only write a demo of the string, and the numeric Boolean value is similar to the object creation by the number (), Boolean () constructor.

Return to the property access expression, if the expression follows "." and an identifier, it looks for the value of the property specified by the identifier and returns it as the value of the entire expression. If an object expression follows a heap of square brackets, the value of the expression inside the square brackets is computed and converted to a string , although both methods do not exist if the property is accessed. will return undefine.

We can see that using the "." + identifiers are easier to use, and we sometimes get used to chaining calls more like ".", however, it should be noted that in this way, the name of the property we want to access must be legal. If the property name that we are accessing is a reserved word, or contains a space, or a number, or a value calculated from an expression, then the property access must use square brackets.

Delete operator

Delete is a unary operator that we can use to delete object properties or array elements.

Delete expects his operand to be an lvalue, and if we misuse it so that his operand is not an lvalue, then delete does nothing and returns true, not all of which are capable of deletion, the variables declared by the user Var, the custom function, the function arguments, Built-in core properties such as can not be deleted, delete illegal will throw an error.

Here the insert complements the left value: the so-called Lvalue, the simple point is that can be assigned to the expression, in the ES specification is the internal type reference (Reference) described, its role is to store data space, and storage is allowed. For example, the property access expression here.

Although the delete operator can delete the properties of an object, we know thatdelete simply disconnects the property from the host object and does not manipulate the properties in the property. The delete operator can only delete its own property and cannot delete the inherited property.

A = {B:{c:1}};d = A.b;delete a.b;console.log (D.C)//The result is still 1, and you can see that the delete is simply disconnecting the properties from the host object and not destroying it

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

o = {a:1};d elete o.a; Delete Attribute A and return truedelete o.x; Because a attribute does not exist, nothing is done and returns Truedelete o.tostring; Because ToString is inherited, nothing is done and returns Truedelete 110; No meaning, return truedelete object.prototype; return Falsevar B = 1;delete this.b; Returns falsefunction F () {};d elete this.f; Returns false

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

logical operators
operator Example Description
logos (&& ) expr1 &&EXPR2 if EXPR1   can be converted to false to return EXPR1, otherwise EXPR2 is returned. Therefore, when,  is used in a Boolean environment, it returns true if the two operation results are true, otherwise false is returned.
logos or (| | ) < EM style= "margin:0px;padding:0px;" >EXPR1 | | expr2
Logical non ( ! ) !expr Returns False if a single expression can be converted to true, otherwise true.

In the JS calculation logic we have a short-circuit principle in the use of logical operators. As follows

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

var e = a && 1; 1var F = b && 1; False

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

The above code block variable c assignment value, a and 1 are true, but because of the short-circuit principle of JS, in the logic or as long as the front of a is true, then the back of 1 will be directly ignored, the preceding operand is false will calculate to the following operand. Similarly in the use of logic and, as long as the preceding operand is false, then JS will directly ignore the following operand and let the result of the operation is false, if the previous operand is true, will continue to calculate the following operand, and finally if the subsequent operand is true, the assignment is the subsequent operand.

var a = true 1 | | True 2//true 1var B = False | | True 3//true 3var C = True 4 && True 5//true 5var D = False && True 6/False


JavaScript Small details Dot list

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.