Table of Contents
A list
Home Web Front-end JS Tutorial Javascript learning book recommended_javascript skills

Javascript learning book recommended_javascript skills

May 16, 2016 pm 06:51 PM
javascript

Written by Aaron Gustafson, translated by Li Songfeng, Li Yawen and others. I feel it is definitely a book worth reading. Interested friends can take a look. The following is a collection of common JavaScript pitfalls and object concepts that I extracted from them. Hope it can be of some help to everyone.

Ø Common traps in Javascript syntax

² Case sensitivity

² There is actually no special difference between single quotes and double quotes. I only really knew this after reading this book. Although I had written the string form of '' when writing programs before, I never realized beforehand that "Oh, it turns out that this is also possible."

In addition, in most cases, '' is used to represent strings, because the XHTML specification requires that all XHTML attribute values ​​​​are enclosed in "". This will provide clarity when mixing code.

² Line break, don’t ignore this. Because if you use a carriage return to do a line break in a string, I'm sorry, the browser will tell you that I don't recognize your string. Because it will automatically convert the carriage return into ";" But in order to solve this problem, fortunately it provides an escape character as a replacement. As shown below:

var='

A list







'

Some people will say that you can use a plus sign, I know this. Use the plus sign as a string operator. It is estimated that the bottom layer has reloaded the number (?!).

² Optional semicolons and braces

If you don’t believe me, let me tell you it’s okay. It can be said that this JavaScript is quite smart. But like the author of the aforementioned book, I think it’s better for us as programmers to be more disciplined.

² Overloading

Sometimes you may have a whim and make a JavaScript overloaded function, and you will find that only the last one can run at this time, and the previous ones have not been accepted. Chance. What is the reason for this?

It turns out that the previous so-and-so has been replaced by the later one. This is commonly referred to as coverage. A further step is that the program only references the last function with the same name in the scope chain.

² Anonymous function

I have to say that this guy is very useful.

² 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 along which the value of a variable (or the method to be used when a function is called) can be determined.

A closure is A concept related to scope, which refers to the fact that an inner function can still access the properties of its outer function even after the outer function has completed execution and terminated. When a variable or method is referenced, JavaScript will parse along the scope chain formed by the execution path of the object, looking for the most recently defined value of the variable. Once found, that value is used.

² Iterable object

Don’t doubt that if this is not used well, errors may occur. If you don’t believe it, just look at this example:

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

for(i in all){

//For all[i ] element to operate.

}

Since the values ​​returned will be equal to length, item and namedItem respectively, this may cause unexpected errors in the code.

Something needs to be done at this time. Use hasOwnProperty for property filtering. This function returns true when the object's properties or methods are non-inherited. The method is as follows:

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

for(i in all){

if(!all.hasOwnProperty(i)) {continue;}

//Operation on all[i] elements.

}

² Function calls and references.

Note, this is different, the call will be executed, and the reference will only give a copy to the variable (it seems like this can be understood, right?)

Look at this:

var foo=exampleFunction();

var foo=exampleFunction;

The two sentence patterns are different. The former one executes the function exampleFunction and assigns the return value to the variable foo, while the latter one assigns the reference of the function exampleFunction to foo.

Ø Javascript object

I believe everyone knows the concepts of properties and methods. Let's talk about the objects in JavaScript and their mysterious meanings (it's like martial arts).

1. Inheritance

The inheritance of Javascript makes me feel strange, but after thinking about it, it still makes sense. And it’s still the same idea as the others. In fact, javascript just performs a copy operation. Without further ado, let’s look at an example and I believe everyone will understand.

//Create an instance of person object

var person={};

person.getName=function(){……};

person.getAge=function(){……};

//Create an instance of 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

There are two ways to create your own object:

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

The second type: var myObject={};//is the abbreviation of the first type. In fact, it has been used above.

3. Create a constructor

First type: function myConstructor(a){

//Code

}

No Surprised, imagine that there are objects everywhere in the javascript mentioned above, although it is a bit exaggerated. This function is an object at a time.

The second type:

Perhaps smart readers have already guessed that they are the other two types of function definitions:

var myConstructor=function(a){};

Let’s also write the third one together: var myConstructor=new Function('a',/*some code*/);

But for this method, it will cause performance problem, so it is more appropriate to use function.

Finally, give me an example from the book:

function myConstructor(message){

alert(message);

this.myMessage=message;

}

var myObject =new myConstructor('Instantiating myObject!');

4. Add static method

var myObject={};

//Add attribute

myObject.name=”Jeff”;

//Add method

myObject.alertName=function(){

alert(this.name);

}

//Execution method

myObject.alertName();

I believe everyone can understand it , no more.

5. Want to add public methods to the prototype

The way to add public methods is to use prototype. Note that the prototype here is not the js library.

//Create 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 variables starting with var in the constructor are all private variables. Those that are not added with . and prototype but written directly into the constructor are private functions.

6. Finally, let me mention object literals

Object literals are very helpful for code reconstruction and redundancy reduction. So if possible it is best to use this

. Look at the following example:

var myObject={

propertyA:'value',

propertyB :'value',

methodA:function(){}

}

I have to agree with the author, this method is very elegant.

How about it? Do you have some basic understanding of objects and traps in Javascript? Hope this article is helpful to you.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles