From +new date, the JavaScript unary operator __java

Source: Internet
Author: User

http://zhouhua.github.io/2013/07/09/UnaryOperator/

More

When I accidentally opened D3 's source code, I saw such a piece of coding.

1 2 3 if (! Date.now) Date.now = function () {return + new Date;};

This is generally seen as adding a now method to the date object to get the current time. But return +new Date; Seems to be a very unconventional way of writing, this + is what to use it. In fact, there is no use of very advanced JavaScript skills, on the contrary, just use a very simple unary operator, but this usage is relatively rare, usage and other programming language is too different, so let people have a strange feeling.

Open ECMAScript specification, if not calculate the ~ operator, a total of 8 unary operators. Let's get to the bottom of this. + operator

Let's start with the + operator. Using the method, this operator is similar to the "plus sign" you have seen from junior high school math, and is placed in front of a variable or expression. For a variable of a numeric type, the + operator has no effect on the result, and for other types of variables it is a variable that converts to a numeric type, or a special numeric type constant NaN if the conversion fails. So the op-+a--assumes that a is an object of a non-numeric type--what exactly did you do with a? One argument is to call the ValueOf method by default, which is equivalent to a.valueof (); However, after my specific experiment, it seems that another version is more plausible, namely +a=number (a). -Operator

Like the + operator,-the operator is like a minus sign in math, so we can know-a=0-(+a), the above two symbols are a group. + + operator

This passage will actually contain two operators, similar to a++ and similar ++a. Readers who are familiar with C or Java know the meaning of + +. Indeed, these two operators are emacscript directly from the Java operator, which means adding 1 to the operand, i.e. a++ equivalent to ++a equivalent to a+1. However, the numerical results of the operation of the equivalence does not mean that the two operators are the same, in fact, there are differences between them, I believe that understanding C or Java readers have heard them "first assignment and then" and "first operation and then assign value" difference.

What exactly are their differences? Or with the specific results of the experiment to explain it. JavaScript code

1 2 var a=10; Console.log (a++-1);

Figure 1:a++ Operation result JavaScript code

1 2 var a=10; Console.log (++a-1);

Figure 2:++a Operation result

From the above experimental results, we know that a++-1 executes the A-1 result first, then executes the a=a+1, so it is equivalent to "first compute and then assign", and ++a-1 is different, it is to execute the a=a+1 first, and then use the new A to participate in the Operation A-1, so this is "first assignment and then operation." – Operator

Similarly, this is also the a--and--a two operators, the specific meaning of the opposite with the + + operator, indicating that the operand minus 1, the specific characteristics of the same as the previous paragraph, no longer repeat. Delete operator

The delete here is different from the Delete keyword in C + +, which is not used to free up the memory space of an object, but rather to release a reference to a custom property and method inside the object. For the use of delete, I use a picture to illustrate.

We defined a date-type object A and defined it with a property name and a method speak, and then we use the delete operator to delete the custom properties and methods. We also found that the properties and methods built into JavaScript are not deleted by the delete operator, and here we test the valueof method of the Date object.

Let me throw out a simple question, if we define a variable or function, whether we can delete the variable or function with the delete operator. Why. void operator

The use of the void operator is to return undefined to all expressions, a function that seems odd, and may be useful in preventing unexpected computations ... See more applications, one is as a dead link, one is to open a new window. void operator makes dead link

1 < a href = "javascript:void (0)" > I'm a dead link </a >

I'm a dead link. Open a new window

To open a new window, we can use this code:

1 < a href = "javascript:window.open (' About:blank ')" > Open new Window </a >

I don't recommend this to the reader. link, but I tested it, chrome is done on this issue, if you want to deliberately test the bug, please switch to IE browser to click on the previous link. This time the void operator works, and we change the code to:

1 < a href = "javascript:void (window.open (' About:blank '))" > Open a new Window &L t;/a >

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.