An explanation of the operators in JavaScript 1

Source: Internet
Author: User
Tags hasownproperty

Long time no writing, according to Bo Master's technology, still write a little JavaScript starter article, next we explore JavaScript operators.

First, preface

There are many operators in JavaScript, but many beginners do not understand or misinterpret their use, this chapter will lead beginners to learn some of the common JavaScript operators: typeof, IN, delete, new.

Second, learning objectives

1. In-depth understanding of JavaScript operators: typeof, IN, delete, new functions and usage.

2. Analyze the fundamental, master the use of these commonly used operators scene, ingenious.

Three, the course explanation 1. typeof

The typeof operator output has the following values: Number, String, Boolean, undefined, object, function, but how to determine what value typeof output is not easy, which combines a lot of JS's other basic knowledge, Let's look at the code here:

varA=NaNvarb=NULLvarC=NewString ('123')varD= []vare;typeofA// NumbertypeofB//ObjecttypeofC//ObjecttypeofD//ObjecttypeofE//undefinedtypeofF//undefined

Conclusion:

Nan is a non-numeric value, although his meaning is not a numeric value, but his type is still a numeric type, which represents the number of non-numeric values only.

Null for historical reasons, its typeof is still object,null represents a null pointer object, and the top-level object.prototype.__proto__ of the prototype chain points to null

New String/number/boolean All three create an object that is not the same as the direct assignment literal.

typeof operation undefined variable will not error, but return undefined, so in the code, we often use TypeOf to determine whether a variable exists, so as to avoid the variable does not exist to cause an error.

2. In

The most common way to traverse object key values is for...in, but do you really understand in? If you do not understand, then enter this section of the study.

For in execution, which is related to the prototype chain, the in operator finds the entire prototype chain of the object, and he finds and outputs enumerable properties and methods in the prototype chain (not within the scope of the prototype chain and descriptor). To avoid for...in traversal of properties or methods on the prototype chain, we often see a judgment: O.hasownproperty, which determines whether a property is an attribute owned by the object itself, not an inheritance.

varFoo =function(name) { This. Name =name} Foo.prototype.hello=function() {Console.log ( This. Name + ' Hello '}varf =NewFoo (' Xu ') for(varKeyinchf) {Console.log (key)//output name, hello} for(Keyinchf) {if(F.hasownproperty (key)) {Console.log ( key)//Output Name  }}

In can also be used to judge whether an object exists or not, an attribute can be judged by in, an expression: Property in Object (for example: ' name ' in f)

3. Delete

So the name Incredibles, used to delete the data, he can only delete the object's property content or an array of some subscript element, he cannot delete the defined variables including objects and arrays, delete succeeds returns True, otherwise false. As shown below:

varA = 1varb = [1,5]varc ={name:' Xu ', hello:function() {Console.log (' Hello ')  }}DeleteA//falseDeleteARR[1]//trueDeleteC.name//trueConsole.log (a)//1Console.log (b)//[1,undefined]Console.log (c)//{hello:function () {...}}

In JavaScript, any variable defined by Var cannot be deleted with the delete operator (the variable defined in ES6 is not visible to delete let/const). The JavaScript parser initializes the configurable descriptor of the variable defined by Var to false, so it cannot be deleted successfully. The deleted array elements are populated with undefined. Object has no traces left after its properties are deleted, but only if the properties are configurable.

4. New

The new object that we used to create is actually very simple, but there are some potential pitfalls as follows:

var function (name) {  this. Name = name;    return {    ' Xu '}  }varnew Foo (' Xu ') console.log (f.name)   // undefined
Console.log (name in f)//Falseconsole.log (f.user) // ' Xu '

In the example above we define a constructor foo, pass in a parameter name, write the incoming name to the instance inside the constructor, and finally return an object {User: ' Xu '}. So the question is, new Foo is supposed to be returning an instance of Foo, which means that f.name is there and the value in the case is ' Xu ', but we get undefined, and name in F returns false, which means the object does not have a name attribute. And F.user got the ' Xu '.

As you might guess, if an object is returned in the constructor, the constructor returns the defined object in the new procedure, rather than returning an instance of the constructor. So, what if the return is a number or a string?

var function (name) {  this. Name = name;    return 1;} var New Foo (' Xu '//  {name: ' Xu '}

In this case, we can see that if the return of the constructor is a number, then this constructor of new will be ignored and the correct constructor instance object is still returned. Do not believe you can use instanceof to verify.

End

The above is the content of this article, I hope to be helpful to the reader, if there are errors please correct me ~ ~ ~

An explanation of the operators in JavaScript 1

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.