The first and second sections of this series introduce JavaScript module prototypes and theoretical concepts, and today we describe how they can be used in combat.
I'm using a very popular library require.js.
First, why use Require.js.
At the earliest, all of the JavaScript code was written in a file, and it was enough to load the file. Later, more and more code, a file is not enough, must be divided into
(Number ))// Equivalent to Array. apply (null, Array (3). map (Function. prototype. call, Number)
Although user-defined functions are clearer, user-defined functions are certainly not faster than native functions. For example:
The Code is as follows:
Var a = ["aaa", "bbb", "ccc"]A. map (function (x) {return x. trim () ;}); // ['aaa', 'bbb ', 'ccc']A. map (Function. prototype. call, String. prototype. trim); // ['aaa', 'bbb ', 'ccc']
The preceding map method is used to trim the space of eac
Random sorting of JavaScript learning notes array and javascript learning notes
Recommended reading:Array Summation Method for JavaScript learning notes
Add, delete, modify, and query the array of JavaScript learning notes
The sort () and reverse () methods are provided in JavaScri
objects cannot be contaminated in module code, suchJs CodeString. prototype. func = new function (){...};Such code must be centrally controlled, for example, uniformly placed in common. js and strictly protected.Data storage constraints:Common variables, prototype variables, and function variables are separated and governed. The method names start with uppercase. The variable names still follow the camel naming method:Java codeFunction T (name ){T. prototype. _ instance_number ++;This. name = n
, Moment. js, a popular date conversion and formatting class library, can regard it as commensurate, because its API surface is compact and it exactly matches the purpose of the class library. Moment. js is used to process dates. Its API provides convenient functions to process javascript Date objects:
?
1 2
Moment (). format ('ddddd '); Moment (). startOf ('hour'). fromNow ();
For a targeted class library like Moment. j
Six de-duplicated JavaScript Array methods, javascript Array
Method 1
Without thinking, we can get the O (n ^ 2) Complexity solution. Define a variable array res to save the result and traverse the array to be de-duplicated. If this element already exists in res, it indicates it is a duplicate element. If it does not exist, it is placed in res.
function unique(a) { var res = []; for (var i = 0, len = a.len
paradigms.
The confusion at the second level is a bit subtle. Here, some JS developers pursuing the trend are secretly thinking about it.
Okay, nonsense! But I already know all the knowledge and skills about functional programming. I use Underscore. js on all my projects.
Underscore. js is a popular JavaScript library that is used everywhere. For example, I have
JavaScript:Start with the letter, underscore ' _ ' or dollar sign ' $ 'Allow names to contain letters, numbers, underscores ' _ ' and dollar sign ' $ 'Case Sensitivenames of variables, parameters, member variables, functions begin with lowercase letters, and the constructor's name begins with an uppercase letter. Variables that begin with the underscore ' _ ' are generally used to identify private/local me
Chapter One JavaScript modular programming(a): How the module is writtenA primitive notationA module is a set of methods to implement a specific function, as long as the different functions (and the variables that record the state) are simply put together, even if it is a module;function M1 () {// ...}function m2 () {// ...}The above functions M1 () and M2 () to form a module;Disadvantage: "Pollution" of the global variables; There is no guarantee tha
This article mainly helps you understand how Map replaces loops in javascript. Interested friends can refer to this article to introduce the benefits and convenience map brings to our js programming:
1. What can Map do?Map can implement the for loop function:
《script》 var arr = ['val1', 'val2', 'val3']; for(var i = 0; i
The advantage here is that we can write functions in map at will, which will greatly improve the code readabili
Javascript basics-Understanding javascript and javascriptJavascript basics-Understanding javascript
Every language begins with HelloWorld. I also follow this principle today! First, let's get to know about javascript.
This is a very simple program. When you open the page, the alert method will be called. A prompt box
. For larger and broader class libraries, the size of the API should reflect the capabilities of the class library.
Taking Underscore as an example, as a library for multiple functions, it provides a large number of convenient functions that are designed to help developers process javascript collections, arrays, functions, and objects. Its API volume far exceeds that of libraries such as Moment. js, but
comments
This annotation method is often seen in JavaScript code. What is the use of comments like "initialize valueA to be sero? Can't the engineer who reads the program find out from the "var valueA = 0;" copy statement? "Set timeout to be 20 s" is not only a copy or paste error, but also misleading the programmer's understanding of this statement. The setTimeout () function is not used to set the function execution timeout, but to wait for a c
JS has always been a smattering of knowledge, recently encountered this aspect of the problem, so on the internet to learn a bit, and now do not fully understand, first posted notes;
The first chapter JavaScript modular programming
(i): the formulation of modules
A primitive writing//module is a set of methods for implementing a particular function; Simply put the different functions (and the variables of the record state) together, even if it is a
its API surface is compact, and it matches the purpose of the class library explicitly. Moment.js is used to process dates, and its APIs provide handy features for working with JavaScript date objects:
12
moment().format(‘dddd‘);moment().startOf(‘hour‘).fromNow();
For a targeted class library, like Moment.js, it is important to keep the API focused and simple. For larger and broader class libraries, the size of the API
name variables and functions, or to mark the jump position in some loop statements in javascript code. The javascript identifier must be a letter. Start with an underscore or dollar sign. The subsequent characters can be letters. Number. Underline or dollar sign (numbers are not allowed to appear as the first letter, once ja
Common JavaScript Array Operation tips, javascript Array
This example summarizes common operations on JavaScript arrays. Share it with you for your reference. The details are as follows:
Preface
I believe everyone is familiar with array-related operations commonly used in these class libraries such as jquery or underscore
in a stackoverflow Q A,... The difference between in and for (;) is that the former represents the attributes of enumeration objects, and there are two problems:
The order of enumeration cannot be guaranteed;
The inherited attributes are also enumerated;
InArray. prototype. forEachIn this table, we can also clearly see that IE8 and earlier versions cannot be accurately supported:
The compatibility of the forEach method is described in detail here. In fact, the main
.Listing 2. Meaningful commentsThis annotation method is often seen in JavaScript code. What is the use of comments like "initialize valueA to be sero? Can't the engineer who reads the program find out from the "var valueA = 0;" copy statement? "Set timeout to be 20 s" is not only a copy or paste error, but also misleading the programmer's understanding of this statement. The setTimeout () function is not used to set the function execution timeout, bu
1. ConstantsYou can use the Const keyword to create a read-only named constant. The constant name must start with a letter, underscore, or dollar symbol, and the remainder can contain letters, numbers, or underscore characters:Const AREA_CODE = ' 515 ';The value of a constant cannot be modified by assignment or re-declaration, and the constant must be initialized to a value.JavaScript supports the following
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.