JavaScript complements-(ii) statements and strict patterns, objects

Source: Internet
Author: User
Tags try catch

Third, statement and strict mode

A JavaScript program consists of statements that follow the syntax rules.

For example: If statement, while statement, with statement, etc...

Block Blocks Statement

Often used to combine 0~ multiple statements, block statements defined with {}

Directly beginning with curly braces is understood as a block, and if you want to be understood as an argument, you need to assign a value or set a parenthesis

There is no block-level scope to be aware of:

Declaration statement Var

It is important to note that we can define multiple variables within a var statement.

such as Var a=1,b=1

But you can't use Var a=b=1

B would be an implicit global variable

The try catch provides an exception-catching mechanism

The execution of the process is to first execute the code in the try block, if an exception is thrown, caught and executed by a catch, if no exception is ignored, finally the statement will be executed, after the try must follow a catch or finally

In the case above, there is no catch in the first block, so it jumps to find the nearest catch, and before jumping out, he executes the finally inside.

In this case there is a catch, run out of the exception will be the first to execute the inside catch, and then execute finally, because a one in the inside has been processed, so no longer run outside the processing

On the back of this, the order to the right, the internal try to throw an exception, and then catch processing, finally, and then because the catch and throw the exception, so the outside in the processing, that is, if the exception is not handled thrown, it will be processed outside, but first execute the inside of finally

Function statement

A function statement is used to define a function object, and functions are defined as a function declaration, and the corresponding one is called a function expression, as follows

The main difference is that the function declaration is pre-processed, or the function is called pre-

You can also create a function object by means of the new function constructor

For ... in statement

Here we can walk through the properties in obj with for-in

Switch statement

There are three ways

Looping statements

With statement

You can modify the current scope

As below, with is not recommended in JS because scopes in with are more complex

Strict mode

Strict mode is a special mode of execution that fixes some of the language deficiencies, provides stronger error checking, and enhances security

Can be up-compatible if the old browser is incompatible and will be ignored as a string

Note that it is not necessarily the first line of code, and then he can be preceded by quotation marks to ABC, but not with Var in front

The main differences in strict mode are:

Use with or error in strict mode, syntaxerror syntax errors

A variable that is not declared will be assigned a value error.

Strict mode is a special operating mode that fixes some of the language deficiencies, provides stronger error checking, and enhances security

Strict mode is up-compatible

Iv. objects

Objects contain a series of attributes that are unordered, each with a string key and a corresponding value.

The string key of the object

The construction of the object

Each object has a prototype in addition to tags and methods

For example, we create a function foo

Each function will have a prototype such an object property

If we do, Foo.prototype.z is set to 3

Go to New one Foo point to an object obj, whose prototype will point to the prototype property of his constructor, which is Foo.prototype, which we set before Foo.prototype.z is 3

If we access the X property of obj, we will return 1, if we access the Z property of obj, we will find the prototype of obj, and if not, we will continue to look up until the end of the prototype chain.

There is also a class tag in the object to indicate which species he belongs to.

Extensible tags to indicate whether new attributes are allowed to continue to be added

Create objects, prototype chains

To get to the object, you have to have the object literal to create the object

We can set the property in curly braces, object literals can be nested objects, such as the value of some objects are objects

New prototype chain

If we use function to create an object Foo, he will take a prototype attribute, which is an object property, give him a Z value equal to 3

Create a new variable, obj, with new constructor, add two properties to the obj assignment

Constructing objects with new

Its main feature is that its prototype will point to the prototype property of her constructor, which is Foo.prototype

The effect is that when we visit obj.x and OBJ.Y will return his value, when he accesses obj.z he goes back to find its prototype is foo.prototype, return z value 3

Foo is also a prototype, it will point to the Object.prototype,object.prototype and will point to its prototype null

The last sentence returns false to indicate that Z is not his own, but that it is on the prototype chain.

It is important to note that when we assign a value to an object, it does not look like a prototype chain.

If he has this attribute, he will modify it, and if not, he will add this attribute

This time we were watching, Obj.z is 5,foo.prototype.z is 3

If we assign a value of undefined to obj.z, the lookup will return undefined

So returning undefined is not necessarily no, maybe his value is undef.

If you want to get Z on the prototype chain, you need to delete obj.z first.

Delete Obj.z does not delete the prototype chain

Object.create creating objects

This is a system built-in function, he will accept a parameter, is generally an object, it will return a newly created object, and let the object's prototype point to this parameter

Not all objects have ToString, and not all objects have object.prototype on the prototype chain.

Property manipulation

Read-Write Object properties

We could have. operator to access his object properties, you can also use "" to pass his key in, here we need a string, we can also write other, but also will be converted to a string

General recommendations in the form of obj.

But as to the right, there are x1 x2 ... Here we may need to loop dynamic processing, we need to spell this property name, often use ""

In addition to using for to spell, we also use for in to go through all the properties, but also can be the prototype chain of properties also traversed, and the order is indeterminate

When we try to read and write non-existent properties

For example, we create an object, obj, access a nonexistent property Y, a prototype chain lookup, find the end of the prototype chain NULL, or it will return if it is not found underfined

If you write this property, modify the value of the object if there is one, and if not, create and assign a value

Obj.y is undefined, then go to visit obj.y.z will error can not get underfined attribute Z, return to assign value

Using operators to implement chained reading

Define the variable yz, want to go to the obj.y to operate, obj is not empty, but want to detect whether the Y property is sometimes, will use if, the y.z out to do some processing

Skillfully with the operator, && if there is something, continue to the right to find, if there is a underfined will return, always find the last, if the obj.y.z exists will return Z value

Delete Property

Use Delete to delete to return true to delete success

You can also use [] to pass in the Delete property

If the deletion is repeated, will there be a problem, because the property is no longer present, JavaScript will still return true, so delete returns true only means that this property is not, does not mean that the deletion succeeded

Some properties, such as prototype, are not allowed to be deleted and will return false

We can try to pass in the object to view, to see the property prototype

The following meanings are configurable, not

If the variable or local variable defined with VAR can still not be deleted

The function declaration is also the same, or the function of the local scope inside the function, can not be deleted

If the diet creates global variables and does not define Ohno, we can use window. To get 1,delete successfully deleted

Detection

Constructs a variable cat with the new object, adding two attributes by assigning the value,

In detection, in will find the prototype chain, so cat's property legs and ToString on the prototype chain will return true, but ABC will return FALSE if it does not exist.

With Hasownprototy

Finally, is it possible to enumerate with prorerisenumerable

How to customize a property so that his enumeration is false,

We want to multiply the number of cat legs by two, first using if to view cat and cat. Legs converts a Boolean value that is not false when it is said to be equal to 2

Or!=underfined, an equal to null and underfined is to wait, that is, not equal to NULL in doing the following processing

If it is not equal to underfined, use!==

Enumeration properties

Define variable o, with X y z three properties

ToString on the prototype chain O or O

Non-enumerable

Define variable key with in won't come out

If we create a variable obj with Create, the prototype points to O, adds an attribute a to obj, and the value is 4, which enumerates

Traversal, either a or XYZ, is displayed in the traversal.

Sometimes you just want to deal with the properties on the variable and don't want to handle the properties on the prototype chain

Only one has to be judged to filter out the properties on the prototype chain.

Getter,setter Methods for attributes

You can see that these two methods differ greatly from other methods.

Property labels

View the property label for a variable

Manage or Set properties

Object label

There are three main types

Proto class extensible

Proto is actually a prototype.

class indicates which type of object is

cannot be obtained, can indirectly

Extensible indicates whether it is possible to expand

Serialization of other object methods

Serializing a custom

ToString and valueof are also common object methods

JavaScript complements-(ii) statements and strict patterns, objects

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.