Javascript Learning Book recommended _javascript skills

Source: Internet
Author: User
Tags closure hasownproperty
Aaron Gustafson, Li Song Li Yaven and other translations. The feeling is definitely a book worth reading. Interested friends can have a look. Here's what I've sorted out from the JavaScript common pitfalls and objects that I extracted from them. I hope we can help you.

a common pitfall in Øjavascript grammar

² Case Sensitivity

² single and double quotes actually do not have a special distinction, this point I have read this book began to really know, although in the previous writing program, "the string form, but there is no prerequisite of the discovery of sex" Oh, it is also possible. ”

In most cases it is also used to represent strings, because the XHTML specification requires that all XHTML attribute values be enclosed in "". This can be very clear when mixing code.

² To change lines, do not ignore this point. Because if you use a carriage return in a string to do the line, then the browser will tell you that I do not know your string. Because it automatically converts the carriage return to ";" But in order to solve this problem, fortunately it provides an escape character as an alternative. As shown below:

Var= '


<ol>\

<li class= "a" ></li>\

</ol> '

Some people would say that they can use the plus sign, which I know. Use the plus sign as the operator of the string. Estimated is the bottom of the + number of overloads (?!) )。

² Optional Semicolon and Braces

If you don't believe me, I tell you it's OK. This can be said that JavaScript is still more intelligent. But like the author of the book, I think it would be better for us to be a programmer or a little more orderly.

² Overload

Sometimes you might have a whim to do a JavaScript overload function, you will find that at this time only the last one can be run, the front is not get the opportunity to hire. What's the reason?

It turns out that the previous So-and-so has been replaced by the back. Which is usually said to cover. Further, the program refers only to the last function of the same name in the scope chain.

² anonymous function

I have to say this guy is very useful.

² scope resolution and Closure

This scope is believed to be familiar to everyone, because each programming language has such a concept.

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

A closure is a scope-related concept that refers to an intrinsic function that can still access the properties of its external function even after the execution of the outer number is completed and terminated. When referencing a variable or method, JavaScript parses the value of the variable's most recent definition along the scope chain that is composed of the object's execution path. Once found, this value is used.

² Iteration Object

Do not doubt that the use of bad words, it is likely to produce errors. If you don't believe it, look at this one example:

var all=document.getelementsbytagname (' * ');

For (I, all) {

Action on the all[i] element.

}

Because the returns are equal to length, item, and Nameditem, this is likely to cause an unexpected error in your code.

It's time to do some processing. Property filtering with hasOwnProperty, which returns True when an object's properties or methods are inheritable. The method is as follows:

var all=document.getelementsbytagname (' * ');

For (I, all) {

if (!all.hasownproperty (i)) {continue}

Action on the all[i] element.

}

² the invocation and reference of the function.

Note that this is different, the call will execute, and the reference will only give the variable a copy (seems to be the way to understand it?). )

Look at this:

var foo=examplefunction ();

var foo=examplefunction;

These two sentences are not the same. The first is to execute the Examplefunction function and assign the return value to the variable foo, and the latter is to assign the reference to examplefunction this function to Foo.

Øjavascript Objects

The concept of attributes and methods I believe we all know. Here's a list of the objects in JavaScript and the metaphysical mysteries of them (speaking of martial arts).

1. Inheritance

I'm surprised by the succession of JavaScript, but I think it makes sense after thinking about it. And the same thoughts as the others. In fact, JavaScript is doing a copy of the operation. Gossip does not say to see an example I believe everyone will be clear.

Create an instance of a person object

var person={};

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

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

Create an instance of an Employee object

var employee={};

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

Enployee.getsalary=function () {...};
Inheriting methods from the person object

Employee.getname=person.getname;

Employ.getage=person.getage;

2. Create your Own objects

There are two ways to create your own objects:

The first type: var myObject =new Object ();

The second type: Var myobject={};//is the first shorthand form. Actually, the noodles are already in use.

3. Creating constructors

The first type: function Myconstructor (a) {

Code

}

Don't be surprised, imagine that the JavaScript in front of you is full of objects, albeit exaggerated. This function is a temporary object.

The second type:

Perhaps a wise reader has guessed that there are two other types of function definitions:

var myconstructor=function (a) {};

The third is also written together: Var myconstructor=new Function (' a ',/* Some code * *);

But in this way, because it can cause performance problems, it is better to use function.

Finally, give an example of a book:

function Myconstructor (message) {

alert (message);

This.mymessage=message;

}

var myObject =new myconstructor (' instantiating myobject! ');

4. Add static method

var myobject={};

Add properties

Myobject.name= "Jeff";

Add method

Myobject.alertname=function () {

alert (this.name);

}

Execution method

Myobject.alertname ();

I believe we all can see, do not say.

5. To add a public method to the prototype

The way to add public methods is to use prototype, note that the prototype here is not that JS library.

Creating constructors

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 the variables in the constructor that start with VAR are all private variables, not add to the. and prototype, but write directly to the constructor as a private function.

6. Finally, mention the literal volume of the object

Object literals are helpful for refactoring the code and reducing redundancy. So it's best to use this if possible.

Look at the following example:

var myobject={

Propertya: ' Value ',

Propertyb: ' Value ',

Methoda:function () {}

}

Have to agree with the author's point of view, this way is very elegant.

What do you think? Do you have some basic knowledge of the objects and traps in JavaScript? I hope this article will be of some help to you.

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.