js tutorial
What can JavaScript do?
JavaScript can change HTML elements content.
1 | <p id="demo">JavaScript can change HTML content.</p> |
JavaScript can change HTML elements attributes.
1 | <button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button> |
JavaScript can show and hide HTML elements.
1 | <p id="demo">JavaScript can hide HTML elements.</p> |
1 | <p id="demo" style="display:none">Hello JavaScript!</p> |
JavaScript can change CSS style.
1 | <p id="demo">JavaScript can change the style of an HTML element.</p> |
JavaScript Insert
internal element
<head>1
2
3
4
5
6
7<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>internal element
<body>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<body>
<h2>Demo JavaScript in Body</h2>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>external URL
1
<script src="https://www.w3schools.com/js/myScript.js"></script>
external file
1
<script src="/js/myScript.js"></script>
Call JS Through Python
1 | function f(){ |
1 | import execjs |
Variables
We can use var, let, const to define a variable.
1 | var _a = '1', $b = 2, |
var
Firstly, we should know JS interpreter will declare all
varvariables at beginning no matter they declares at the end of the file.1
2
3console.log(a) //undefined
var a = 100vardefines a local variable.1
2
3
4
5
6
7
8
9function test(){
var a = 1;
b = 2;
}
test()
console.log(a) //ReferenceError: a is not defined
console.log(b) //2let
The
letvariables solve the weak point ofvarvariables that can be use before declare them.
operators
equal
1 | // Is value equal? |
and, or, not
1 | console.log(true && false) //false |
Data Types
Primitive types
Number, String, Boolean, null, undefined, NaN are the primitive types.
There are some differences between assigning the primitive variables and object variables.
1 | var a = 10 |
quote type
Object is the quote type.
1 | var c = { |
**The reason is that the value of an object variable is the object’s address. **
Condition
1 | if (1 > 2){ |
Function
the address of a function
1 | var a = function (){ //anonymous function |
the returning value of a comma expression
Comma expressions are evaluated from left to right, and the result is the value of the last expression
1 | function f(){ |
the hidden argument
There is a hidden argument in a function.
1 | function f(){ |
Immediately Invoked Function Expression(IIFE)
We can add two sets of parentheses to write an IIFE: one to define the function, and the other to invoke it.
1 | (function (){ |
Function Hoisting
The JS interpreter declares the function firstly and then invokes themn.
1 | f() //World |
this
1 | function f(){ |
As you can see, the address of this is the address of previous function.
call()
call(target, arg1, arg2): the call function can change the address of this
1 | function Teacher(name, age, major) { |
setTimeout(), setInterval()
setTimeout(function name(params){}, ms)
1 | y = setInterval(function(){ |
we can use cleanInerval(number) cleaning the timer.
eval()
The eval() can execute the string type of JavaScript code.
1 | s = "console.log('Hello World')" |
But the string type of JavaScript usually is encrypted.
Arrow function
The arrow function is a omission of general function.
1 | a = function (){ |
Loop
The i<10, j<15 is a comma expressions that return the value of the last expression.
1 | for(i=0, j=0; i<10, j<15; i++, j++){ |
1 | a = ['a', 'ferry', 1, 'reverse engineering'] |
Date
1 | var t = Date.now() |
Object
like dictionary object
An object consists of variables, functions, and objects.
1 | var obj1 = { |
JS confusion
We have to focus on the second method accessing variables and functions. The string type can refer to the name of them. Therefore, we can change the form of the string, such as encryption
Encryption
1 | //First of all, we encrypt the string substring |
Decryption
1 | s4 = [ |
JS confusion
1 | //Origin JS |
Constructor
We can use Teacher constructor function to create objects.
1 | function Teacher(name, age, major){ |
prototype object
Sometimes, we need to add functions to the original object. Therefore, we need to use prototype keyword.
1 | //prototype object |
Teacher.prototype is an object belonging to a attribute of Teacher constructor function. It acts as the prototype of all instances created using new Teacher().
1 | Teacher.prototype === ferry.__proto__ |
ferrt.__proto__ is an object belonging to instances created using new Teacher(). It points to the prototype of its constructor function.
ferry.__proto__ —> Teacher.prototype, Teacher.__proto__ —> Function.prototype
Besides, Teacher.prototype.__proto__ —> Object.prototype
┌────────────┐
│ Function │ ← 所有函数的构造器
└─────┬──────┘
│
▼
┌──────────────────┐
│ Teacher │ ← (构造函数)
│ __proto__ ─────────▶ Function.prototype
│ prototype ─────────▶ (Teacher.prototype)
└──────────────────┘
▲
│
┌─────────────┴─────────────┐
│ Teacher.prototype │
│ (一个普通对象) │
│ __proto__ ─────────▶ Object.prototype
└─────────────┬─────────────┘
│
▼
┌────────────────────────┐
│ ferry │ ← 实例对象
│ __proto__ ─────────────▶ Teacher.prototype
└────────────────────────┘
jQuery
CSS selector
Using CSS selector to select the element
1 | $('#name')[0] |
ajax() method
we need to pass a object as the parameter for ajax method
1 | $.ajax({ |
Scope
The Scope mains the effectiveness range of variables or functions in the code.
Range: block scope < function scope < global scope
Priority: block scope > function scope > global scope
var
Variables declared by var have a function scope.
1 | var a = 3 //variable a is in the global scope. |
let
Variables declared by let have a block scope.
1 | function f(){ |
declare by noting
Variables declared by nothing have a global scope.
1 | function f(){ |
Closure
There is a encrypt function in another function scope. We cannot access that encrypt function outside of the function scope.
We have to record the address of that encrypt function so that we can access it outside its original scope.
There are two method to record:
using a global variable
1
2
3
4
5
6
7
8(function f(){
function encrypt(){
console.log("This is an encrypt function.")
}
global_variable = encrypt
})()
global_variable() //This is an encrypt function.return the address directly
1
2
3
4
5
6
7
8global_variable = (function f(){
function encrypt(){
console.log("This is an encrypt function.")
}
return encrypt
})()
global_variable() //This is an encrypt function.
