Javascript learning book recommend_javascript skills

Source: Internet
Author: User
Tags hasownproperty
Some time ago I read a javascript book titled JavascriptDOM high-level Program Design translated by JeffreySambells and Aaron Gustafson and Li songfeng Li yawen. It is definitely a book worth reading. For more information, see. Below are the common javascript traps and object concepts I extracted from them. Hope to help you.

Common traps in Javascript syntax

2. Case Sensitive

2. There is no special difference between single quotation marks and double quotation marks. I have read this book before actually knowing it, although I used to write a program in the string format of '', I didn't find it first."

In most cases, ''is used to represent strings, because the XHTML specification requires that all XHTML attribute values be enclosed. In this way, the mixed code is clear.

2. Never ignore the line feed. If you use carriage return for line breaks in a string, the browser will tell you that I don't know your string. Because it will automatically convert the carriage return to ";", but to solve this problem, it provides an escape character as an alternative. As follows:

Var = 'a list \

    \

  1. \

'

Some people may say they can use the plus sign. I know that. Use the plus sign as the string operator. It is estimated that the + number is overloaded at the underlying layer (?!).

2. Optional semicolons and curly braces

If you don't believe it, I will tell you that it is okay. It can be said that javascript is still smart. But like the author of the book above, I think it is better to be a programmer.

2. Heavy Load

Sometimes you may be eager to create a javascript overload function, and you will find that only the last function can be run at this time, and none of the above will be hired. Why?

Previously, XX was replaced by another one. That is, overwrite. Further, the program only references the last function with the same name in the scope chain.

2. Anonymous Functions

I have to say this guy is useful.

2. Scope resolution and closure

I believe everyone is familiar with this scope, because every programming language has such a concept.

The scope chain is used to describe a path. The value of the variable can be determined along this path (or the method used when the function is called)

A closure is a concept related to the scope. It refers to the internal function that can still access the attributes of its external function even after it is executed and terminated. When a variable or method is referenced, javascript parses the scope chain consisting of the object execution path to find the value recently defined by the variable. This value is used once it is found.

2. Iteration object

Do not doubt that this is not a good use, it is likely to produce errors. Let's look at this example if you don't believe it:

Var all = document. getElementsByTagName ('*');

For (I in all ){

// Operate the all [I] element.

}

The returned values are equal to length, item, and namedItem, which may cause unexpected errors in the code.

In this case, we need to handle it. Use hasOwnProperty to filter attributes. This function returns true if the attributes or methods of an object are not inherited. The method is as follows:

Var all = document. getElementsByTagName ('*');

For (I in all ){

If (! All. hasOwnProperty (I) {continue ;}

// Operate the all [I] element.

}

2. Call and reference functions.

Note that this is different. The call will be executed, and the reference will only give the variable a copy (as if you can understand it like this ?)

Take a look at this:

Var foo = exampleFunction ();

Var foo = exampleFunction;

These two sentence types are different. The first one is to execute the function exampleFunction and assign the return value to the variable foo, while the other one is to assign the reference of the function exampleFunction to foo.

Ø Javascript Object

I believe everyone knows the concept of attributes and methods. Next, let's talk about the objects in javascript and Xuan aomiao (like martial arts ).

1. Inheritance

The inheritance of Javascript makes me very strange, but it still makes sense after thinking about it. And it is the same as other ideas. In fact, javascript is a copy operation. I believe everyone will be clear about this example.

// Create an instance of the person object

Var person = {};

Person. getName = function (){......};

Person. getAge = function (){......};

// Create an instance of the employee object

Var employee = {};

Employee. getTitle = function (){......};

Enployee. getSalary = function (){......};
// Inherit the method from the person object

Employee. getName = person. getName;

Employ. getAge = person. getAge;

2. Create your own object

You can create your own objects in either of the following ways:

First: var myObject = new Object ();

Var myObject ={}; // is the first abbreviated form. Actually, it is used.

3. Create a constructor

First: function myConstructor (){

// Code

}

Don't be surprised. Imagine that javascript is full of objects, though exaggerated. This function is an object at the moment.

Second:

Maybe a smart reader has guessed the following two types of Function Definition:

Var myConstructor = function (){};

The third method can also be written together: var myConstructor = new Function ('A',/* some code */);

However, this method may cause performance problems, so it is better to use function.

Finally, let's give an example in the book:

Function myConstructor (message ){

Alert (message );

This. myMessage = message;

}

Var myObject = new myConstructor ('instantiating myObject! ');

4. Add static methods

Var myObject = {};

// Add attributes

MyObject. name = "Jeff ";

// Add Method

MyObject. alertName = function (){

Alert (this. name );

}

// Execution Method

MyObject. alertName ();

I believe everyone can understand it.

5. Add a public method to the prototype

To add a public method, use prototype. Note that prototype is not the js library.

// Create a constructor

Function myConstructor (message ){

Alert (message );

This. myMessage = message;

}

// Add a public Method

MyConstructor. prototype. clearMessage = function (string ){

This. myMessage + = ''+ string;

}

One thing to mention here is that all the variables starting with var in the constructor are private variables. Instead of adding the. And prototype variables, they are directly written into the constructor as private functions.

6. Finally, I will mention the literal volume of the object.

The literal quantity of objects is helpful for code reconstruction and reduction of redundancy. So if possible, you 'd better use this

See the following example:

Var myObject = {

PropertyA: 'value ',

PropertyB: 'value ',

MethodA: function (){}

}

I have to agree with the author. This method is elegant.

How is it? Do you have some basic knowledge about objects and traps in Javascript? I hope this article will help you.
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.