In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge about what the basic grammar rules of avaScript are. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.
I. operator
The +-* / operation in js is similar to the c language, and the abbreviation is also similar.
The operation rules of + +,--, + =,% = are also the same.
In js, = = means absolutely equal (the value and type are the same)
! = (not absolutely equal) it is true that there is a difference between the value and the type.
The ternary operator is also supported in js
A > breadth is greater than bread.b is greater than a'.
II. Branch statement
Conditional statements in js:
If Branch:
If if...else... If...else if..else...
Switch Branch:
Switch () {case 1: {} break; case 2: {} break; default: {} / / default} III. Loop statement
For (initial condition; termination condition; variation of cyclic variables)
The for/in loop traverses the properties of the object
While
Do/while
Break
Continue
Very similar to the c language
IV. Capture and handling of anomalies
Catch exception
Try {
}
The statement executed after the exception is caught
Catch (err) {
}
Whether there is an exception or not, the last statement executed
Finally {
}
5. This keyword in js
In an object-oriented language, this represents a reference to the current object. But in JavaScript, this is not fixed, it will change with the change of execution environment. In a method, this represents the object to which the method belongs. If used alone, this represents a global object.
In the function, this represents the global object. In a function, in strict mode, this is undefined (undefined)
In an event, this represents the element that receives the event. Methods like call () and apply () can reference this to any object.
Apply and call allow you to switch the context (context) in which the function executes, that is, objects bound by this, and you can reference this to any object.
The code is as follows:
Document
Var p1 = {firname: "Assad", age: "28", f:function () {return this.firname+ "" + this.age }} var p2 = {firname: "Zhang San", age: "20"} x=p1.f.call (p2) xx=p1.f.call (p1) document.getElementById ("demo") [xss_clean] = x document.getElementById ("demo1") [xss_clean] = xx VI. Let and const define variable usage rules
Const defines constants similar to variables defined using let:
Both are block-level scope
Cannot have the same name as any other variable or function in its scope
There are two other differences between the two:
Constants declared by const must be initialized, while variables declared by let do not
The value of the const definition constant cannot be modified by re-assignment, nor can it be declared again.
The value of the variable defined by let can be modified.
Variables declared by let:
You can use let to define different variables within different {}, but not var, and variables defined by let cannot be redefined using var, and variables defined by var cannot be redefined using let. Const is the same as var, const and let. Global scope variables declared with the var keyword belong to the window object.
Global scope variables declared with the let keyword do not belong to the window object.
Variables declared with the var keyword can be modified anywhere.
Const can define multiple constant values at a time, and each constant value has a different name
The object that const points to cannot be changed, but what the object points to can be changed.
Myarray= ["1", "2", "3"] myarray [0] = "100" / / correct myarray= ["2", "3"] / / error
Constants declared by const can be redeclared and assigned at different block action levels
You can use the Object.freeze () method to freeze variables (both the point and the content are immutable
Both const and let are declared before they are used
The code is as follows:
Document
Variables modified with const will report errors const x2x1x11; x1x11; / / x2x11; document.getElementById ("demo") [xss_clean] = x; document.getElementById ("demo1") [xss_clean] = x1; document.getElementById ("demo2") [xss_clean] = x2; seventh, void links in js
Essential difference between _ javascript:void (0) and # + id
The former will not change the url of the page, while the latter will locate the url to # + id
The syntax format is as follows:
Void func ()
_ javascript:void func ()
Or:
Void (func ())
_ javascript:void (func ())
The most critical word in _ javascript:void (0) is the void keyword, and void is a very important keyword in JavaScript, which specifies that an expression is evaluated but does not return a value. The difference between href= "#" and href= "_ javascript:void (0)" # contains a location information, and the default anchor is # top, which is the top of the page. On the other hand, _ javascript:void (0) simply represents a dead link. When the page is very long, you will use # to locate the specific location of the page. The format is # + id.
If you want to define a dead link, use _ javascript:void (0). You can use _ javascript:void () to execute the desired function, pop-up window, and so on
Grammar rules:
Click me to go back to the top eight, asynchronous programming setTimeout
Asynchronous (async) is a concept relative to synchronization (Synchronous, sync). In the traditional single-threaded programming we learned, the program runs synchronously (synchronization does not mean that all steps run at the same time, but that the steps are executed sequentially in a sequence of control flows). On the other hand, the concept of asynchrony is one that does not guarantee synchronization, that is, the execution of an asynchronous process will no longer be sequentially related to the original sequence.
To put it simply: synchronization is executed in your code order, asynchrony is not executed in code order, and asynchronous execution is more efficient.
A callback function is a function that tells us what to do when we start an asynchronous task: what are you going to do when you finish the task? In this way, the main thread hardly cares about the state of the asynchronous task, and he will finish what he has started. Enable the callback function setTimeout ("function name", interval (milliseconds)) to define the function within the setTimeout function
The code is as follows:
Document
The callback function waits for 3 seconds before execution.
/ / the first method function print () {document.getElementById ("demo") [xss_clean] = "666";} / / here is the timing for three seconds setTimeout (print, 3000); / / the second method setTimeout (function () {document.getElementById ("demo1") [xss_clean] = "666";}, 3000) / / Test / / the main thread is printed first, and the child thread continues to connect to setTimeout (function () {document.getElementById ("demo3") [xss_clean] = "- 1!";}, 3000); document.getElementById ("demo4") [xss_clean] = "- 2!"; console.log ("2"); 9. Function closure
Closures are a mechanism that protects private variables and forms a private scope when the function is executed.
Protect the private variables from external interference. Intuitively speaking, it is to form a stack environment that is not destroyed.
The code is as follows:
Document var add = (function () {var count = 0; return function () {return count+=1;}}) (); function f () {document.getElementById ("demo") [xss_clean] = add ();}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.