The previous article talked about pass-by-value and pass-by-address in js and the scope of functions. In this chapter, we will discuss variables, expressions, and operators in js as well as some js statements. Upgrading...
1, Expression:
The simplest expression: direct quantity or variable name. var a =1;
The value of the direct expression: itself.
Value of variable expression: the value stored or referenced by the variable.
2 , Operator:
Unary operator: For example - 3
Binary operator: For example 3 4
Ternary operator: For example ? : :
Newbie Frequently encountered problems:
Increment operator:
For example:
i = 1;
j = i; // Pre-increment operation, that is, increment the operand first, and then calculate .
//Output i =2; j=2;
i = 1;
j = i; // Post-increment operation, that is, calculate first, and then increment the operand.
//Output i =2; j=1;
3, equality operator:
=: assignment operator;
==: equality operator;
== =: Equality operator;
The value NaN will never be equal to any value, including itself.
alert(NaN == NaN); //false NaN means Not a Number
To detect whether a value is NaN, you can use the global function isNaN();
In addition, newbies should note:
var a =[1,2,3];
var b =[1,2,3];
document.write(a==b); // Output false .(This is actually Chapter 1) content)
// Although the same is the same, the type is the same, but the address is different.
-------------------------------------------------- ---------
var a =[1,2,3];
var b = a;
var c = a;
document.write(b=== c);//Output true;
------------------------------------------ ------------------
var a = "1";
var b = true ;
document.write(a==b); // Output true
document.write(a===b); //Output false ; Same value, different type
4, comparison operator:
It should be noted that the string is processed A comparison.
And it is case sensitive.
If your requirement is case-insensitive:
You can use String.toLowerCase() //Pure lowercase
String.toUpperCase() //Pure uppercase after conversion, and then compare.
5, in operator:
It should be noted that the value on the left is the attribute of the object on the right.
For example:
var a = {x: 1, y: 2};
var b = “x” in a; // true
var c = “toString” in a; // true . The value on the left is a property of the object on the right.
6 instanceof operator:
It should be noted that the operand on the left is an object, and the operand on the right is the name of the object class.
For example:
var a = new Date()
a instanceof Date; // true
a instanceof Object; // true
a instanceof Number; // false
7, 3-element conditional operator:
It should be noted that the first operand must be a Boolean value.
X > 0 ? 3 : 2 ;
8, typeof operator:
It should be noted that since typeof returns object; It is only useful when distinguishing between object and primitive data types.
To distinguish one object type from another, you can use the instanceof and constructor attributes.
9, delete operator:
It should be noted that not all attributes and variables can be deleted.
For example:
Variables declared with the var statement cannot be deleted.
In addition; when delete deletes a non-existent attribute, it returns true; (^_^, this is more funny.)
var a = 1;
alert(delete a); //Return false
alert( delete a.x ); //Return true
Another thing to note:
Delete can only affect the attribute values, and cannot affect the objects referenced by these attributes.
For example:
var my = new Object();
my.height = new Date();
my.width = my.height;
delete my.height;
document.write(my.width);//my.width still refers to the Date object
10, void operator:
One use of void: specifically generate undefined values,
alert( void(0) )
alert( void(1) ) //all output undefined
The undefined here is actually the value after void() operation .
Considering backward compatibility, using the expression void 0 is more useful than using the undefined attribute.
11, exception handling:
Throwing exceptions: throw
Catching exceptions: try / catch / finally
If(x>0)
throw new Error(“x must not be negative!”);
try{
}
catch(e) {
}
finally{ //Always executed last. Usually the elimination operation is performed.
}
12, with statement:
var form = frame[1].document.forms[0];
form.name.value = “ “;
form. address.value = " ";
You can use the with statement instead;
For example:
with(frame[1].document.forms[0]){
name.value = " ";
address.value =” “;
}
Of course, the book strongly recommends not using with, haha. The efficiency is low and there are many problems.
Summary:
Mainly introduces variables, expressions, and operators in js as well as some js statements.
If you still don’t understand, you can search for information on Google. (If you learn to use search, you will be very good.)
Or contact me, you can go to my blog and leave me a message:
In a blink of an eye, you will learn It’s been 3 chapters, I don’t know how you feel about it.
Let’s make do with it. The most important thing to learn is to rely on yourself. If you don’t understand or have questions, write examples and tests immediately to verify. Or look up information. This may leave a deeper impression.
Come on…….