JS programming tips useful _ javascript tips-js tutorial

Source: Internet
Author: User
Javascript is a computer programming language. It is also known as a dynamic language and a parsing programming language. Need to know more 1. the right and right in JS
Javascript is a computer programming language. It is also known as a dynamic language and a parsing programming language. Why is it a script? Because he cannot execute itself, that is, there is no main function or main program entry, and he must be parsed and executed by his host environment, that is, the parsing environment. Why is it a parsing Type? Because JS does not program, Link, assemble, and so on, a system process generates a file and then executes it. It loads and executes the file as a string ..
2. True/false in JS
Null, null, undefined, false, 0, "", '', NaN are all false, and others are true.
3. What are the differences between functions, classes, objects, and constructors?
A: In js, there is no difference between functions, classes, objects, and constructors. We can say that functions are both objects, objects are classes, and classes are constructors. But in general, we call the object an instance of that class to reduce confusion.

The Code is as follows:


Function Person (){
}
Var person = new Person ();
// Person we can call it a function, class, object, constructor (without parameters)
// Person is called an object reference.


4. Understand this keyword. You know JS 1/5..
This always points to the object that calls this method (function). (required)
First, you must know what this is?
This is an object in js. It is an object, not a function. This is an object. You can think of it as an object reference. Who should I reference? The object that references the method.
Where does this object exist? It exists in JS Code and is generally present in functions. If you put this in the script tag, you can try window = this and print what? Why?
Profiling this keyword

The Code is as follows:


Function ready (){
Alert (this = window );
}
Ready ();


What should I do at this time? True. Why?

The Code is as follows:


Function Person (){
This. name = 'zhang san ';
Alert (this = window );
}
New Person ();


What is printed at this time? False. Why?
Lengthen the code by adding var perso =.
Var person = new Person ();
People all over the world know that person references the object generated by new Person. New Person creates a JS instance object, and we can bind attributes and methods.
See the following code.

The Code is as follows:


Fn = function (){
}
Fn. ready = function (){
Alert (this = fn );
}


What do I print when fn. ready () is called? True. Why?
This is a function now. Why?
5. Static methods and attributes in JS will help you.
What is static? Thinking: What is the method that does not move in JavaScript? You do not need to create an instance. You can call the method by class name without any changes. The method is called. No additional code is required. This is just a one-sided statement. Static: it is the characteristics of a class.
In Js, classes are both objects. Why cannot they be directly bound to attributes and methods. Of course.

The Code is as follows:


Fn = function (){
}
Fn. name = 'zhang san ';
Fn. getName = function (){
Return fn. name;
}


This works, but why is it okay to bind all the new processes in this way? Thoughts
6. Influence of prototype and constructor in JS framework
Prototype is a prototype, which is an object .,.
Constructor is a constructor. It is a function.
So far, you need to know when the object is an object and when the object is a function. It is also difficult to use a JS parser to parse and run such flexible code.
Prototype can be said to be a static attribute of the class, which points to the instance of this object. In other words, prototype is an object. What is the difference between the prototype object and the new object? An equivalent bridge is established, but not the same. When we bind an attribute to the prototype function, the attribute and value are bound to the prototype object and are not bound to the object. When you need to access this attribute on the object, the JS parser first looks for this attribute from the object itself, and then finds it on the prototype object.

The Code is as follows:


Function Person (){
}
Var person = new Person ();
Person. name = "James ";
Person. prototype. name = "Li Si ";
Alert (person. name );
Delete person. name;
Alert (person. name );


Constructor points to the constructor of an object. (What is a constructor? Review by yourself .), It can be seen that it is an object-level attribute. That is, to use the constructor attribute, an object is required.
Is prototype a constructor attribute of an object? Of course, since constructor refers to the constructor, is there a prototype attribute? Of course, and so on, the following two pieces of code are correct.
Constructor. prototype. constructor. prototype. constructor ......
Prototype. constructor. prototype. constructor. prototype ......
Actually, I don't know how long it will last? If you are interested, try it. By the way, you can use recursive algorithms.
Among the many JS codes, when we know an object, we need its constructor or a constructor to find its object. Should be started.
7. = and = determine to the end.
= And = the statue is not like it. Do not consider it. You can see at a glance whether it is equivalent to a broken sentence.
What is the difference between wood and wood?
= Determines whether the variables are equal.
=== Judge that the value of the variable is equal.
And so on: ==== why ?? Which is used to report errors. You know. I only want to tell you here, only = and =
JS variables are of a weak type.

The Code is as follows:


Var a = 3;
Var B = "3 ";
Alert (a = B );
The values of variables in JS are strongly typed. You know.
Var a = 3;
Var B = "3 ";
Alert (a = B );


All are var variables, but their values are different. One is an integer and the other is a string.
The comparison between the var type and the var type is true, the ratio of integer to string type, and false.
= Is used to compare whether their values are the same. It is not about the value type, as long as their variables are of the var type. Of course, this is a nonsense. Can you still declare a variable of the second type ?.
===Is used for comparison to determine whether their values are of the same type. If not, there is no comparison. In JS, the values of variables are strongly typed, including integer, String, number, and Boolean.
8. typeof and instanceof completed the mission of failing to judge
Typeof is used to determine the basic data type.
Instanceof is used to determine whether the object type is a certain type number.
More than 9.5 inheritance types make you feel better.
I. Object impersonating
Ii. apply
Iii. call
Iv. prototype
V. for loop mode
Usage: Always remember that inheritance in JS is only a copy of attributes and methods.
10. DOM model structure changes your mind
DOM is an idea that uses data in a tree structure. To learn about DOM, you only need to know that each node has a parent node and 0 to multiple subnodes. each node has a tag representation on the page and a memory corresponds to a tag object. the page only displays the data, and the memory only stores the DOM object data. Any DOM object can have only one parent node object. The parent-child relationship can be changed at any time.
Required:
Get object: Query
Object operations: add, repair, and delete
Content operations: innerHTML, innerText, etc.
Event operation: mouse, key
Style operation: id, tag, class
Attribute operation: attribute
11. Reduce coding by using the callback function
What is a callback function?
In JS, the number of letters is used to identify a function. We can pass a function name (function name handle) to a function, and then the function automatically calls our function to complete related processing.
Callers and callers are separated. We do not need to care about callers or callers. Callback can be used for notification mechanisms and events.
12. Functions and arguments
The function name is the handle of the function, and the pointer and function name are unique, which makes no function overload in JS. Only function coverage. The function name uniquely identifies a function.
Always need to be visited. Function calling in JS is to add brackets () after the function name ()
Function call parameters are optional. In any case, the parameters are stored in the arguments object in the function body. You can use them directly. They are an object and the parameters are stored in arrays.
13. Closures (anonymous functions) provide shortcuts for development
Closures are parameters outside the function called in a function. They are also called anonymous functions, but they are different.
The value of a closure is that it can be used as a function object or an anonymous function. For a type system, this means not only data but code. Most languages that support closures use functions as the first-level objects. That is to say, these functions can be stored in variables and passed as parameters to other functions, the most important thing is that functions can be dynamically created and returned.
Anonymous functions reduce variable name conflicts and provide permission scopes for JS without permission scopes. When we need to provide an interface, but do not want this interface object to be a global variable, we need to use an anonymous function:
(Function (){
Local Code, external access will never be available, unless you provide an entry
})();
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.