JavaScript--Note

1.  Fllow control statements
var a = 21;
  switch(a)
{
              case a == 1 : alert("first ");break;
default: alert("error");
}
2. Input/output
var a = prompt("test");
alert(a);
3. loops
var a=1; do
{
document.write("/r/n");
  document.write(a); a++;
}while (a<100)
4. Ways to include script in html
< html >
< head >< title > Four Standard Ways to include script in html title >
< script type ="text/javascript"src = "2way.js"/>
 
head >
< body >
< script type ="text/javascript">
    function a()
    {
        alert("1 way to include script");
    }
script >
< input value = "1 way to include script"  id ="1 way to include script"type ="button"value ="button"onclick = "a()"/>
< input value = "2 way to include script"  id ="2 way to include script"type ="button"value ="button"onclick = "b()"/>
< a href = "#"onclick = "javascript: alert('Brcue.Lun');return false;"> br a >
body >
html >
Regular Expression
var a= new RegExp("New");
var b = "BruceNew";
alert(b);
alert(b.replace(a,"old"));  ---- à Bruceold
육.Boleans
var inLatinOctal = "/102";


var inLatinHex = "/x42"
var inUnicode = "/u0042";
 
7. Boleans
if(b)
{
    alert("true");
}else
{
    alert("false");
}
var b = 1; --------- à    true;
var b = -1; --------- à    true;
var b = 0; --------- à    false;
var b = false; --------- à    false;
var b = null; --------- à false
var b = true; --------- à    true;
 
8. Create Object
var testObject = new Object();
testObject.id = 1234;
testObject.name = "Bruce.Lun";
alert(testObject.id);
alert(testObject.name);
9. Conversion rules for primitive types
Table 3-5: Result of Conversion to a Boolean
Type
Converted to Boolean
Undefined
false
Null
false
Number
false if 0 or NaN, else true
String
false if string length is 0, else true
Other object
true
 
Table 3-6: Result of Converting to a Number
Type
Converted to Number
Undefined
NaN
Null
0
Boolean
1 if true , 0 if false
String
The numeric value of the string if it looks like a number, else NaN
Other object
NaN
 
Table 3-7: Result of Converting to a String
Type
Converted to a String
Undefined
"Undefined "
Null
"Null"
Boolean
"True"if true , "false"if false
Number
"NaN", "0", or the string representation of the numeric value
Other object
Value of object's toString() method if it exists, else "undefined "
 
10. Promotion of Primitive Data to Objects
var a = "Bruce.Lun";
alert(a.toUpperCase());
alert(a.toLowerCase());//as same as  "Bruce.Lun".toLowerCase()
 
11. javascript five primitive data types
number, string, Boolean, undefined , and null
 
12. & | ^  operator
var a = 5; 0000 0101   AND
var b = 2; 0000 0010
alert(a&b);  -- à output 0;
 
var a = 5;  OR
var b = 2;
alert(a|b); -- à output 7;
 
var a = 5;   XOR
var b = 3;
alert(a^b);  -- à output 6;
 
13. precedence
Precedence
Associativity
Operator
Operator Meaning
Highest: 0
Left to right
.
Object property access
0
Left to right
[ ]
Array access
0
Left to right
( )
Grouping or function or method call
일
Right to left
++
Increment
일
Right to left
--
Decrement
일
Right to left
–
Negation
일
Right to left
~
Bitwise NOT
일
Right to left
!
Logical NOT
일
Right to left
delete
Remove object property or array value
일
Right to left
new
Create object
일
Right to left
typeof
Determine type
일
Right to left
void
Suppress expression evaluation
이
Left to right
* ,/, %
Multiplication, division, modulus
삼
Left to right
+ , –
Addition, subtraction
삼
Left to right
+
String concatenation
사
Left to right
>> 
Bitwise right-shift with sign
사
Left to right
>>> 
Bitwise right-shift with zero fill
사
Left to right
<
Bitwise left-shift
오
Left to right
>, >=
Greater than, greater than or equal to
오
Left to right
=
Less than, less than or equal to
육
Left to right
==
Equality
육
Left to right
!=
Inequality
육
Left to right
===
Equality with type checking (Identity)
육
Left to right
!==
Inequality with type checking (Non-identity)
칠
Left to right
&
Bitwise AND
팔
Left to right
^
Bitwise XOR
구
Left to right
|
Bitwise OR
십
Left to right
&&
Logical AND
십일
Left to right
||
Logical OR
십이
Right to left
? :
Conditional
십삼
Right to left
=
Assignment
십삼
Right to left
*= ,/= , %= , += , –= , <<= , >>= , >>>= , &= , ^= , |=
Assignment in conjunction with preceding operator
Lowest: 14
Left to right
,
Multiple evaluation
 
 
14. local function
function a()
{
    function b()
    {
        return 2;
    }
    alert(b());
}
a();
 
15. Function as Objects
var test = new Function("alert('HelloWorld');");  //Note: F is Capitalized
test();
var test2 = test;  //copy to another object 
test2();
var test = new Function("arg","alert('Hello/t'+arg);");//arg is a argment
test("Bruce");   ---------- à output Hello Bruce
General Syntax :
Var functionName = new Function(“argment 1”,…….”argment n” ,”statements for function body.”);
 
16.Function literals
var test = function(name){alert(name);}
test("Bruce");
 
17.simple class
 
function AutoMan(comm)
{
    this.name = comm;
    this.kick = function (){alert(this.name);}
    this.hit = new Function("alert('Dont hit you brother');");
}
var a = new AutoMan("Ha~ Look this......");
a.kick();
a.hit();
 
18. Advanced  Parameter Passing
function adv()
{
    alert(adv.arguments.length); --- à output 3
    alert(adv.arguments[0]); ---- à output 1
}
adv(1,2,3);
 
19.  Recursion
function   jc(varNumber)
{
document.write(varNumber);
document.write("");
 
    if(varNumber<0)return -1;
    if(varNumber==0)return 1;
document.write(varNumber*jc(varNumber - 1));
document.write("");
    return varNumber*jc(varNumber - 1);
}
document.write(jc(5));
document.write("");
 
20.  JS copy 
var stu = { name:"a", id:"19880904", age: "20",call:function(){alert(this.name);} };
stu.call();
var b = stu;
b.call();
(function(){alert("Pa  you  died ~");})();      =
var a = function(){alert("Pa  you  died ~");}  +
a();
---------------(function(m){alert(m);})(1);   //parameter OK
 
21. clone object
var stu = { name:"a", id:"19880904", age: "20",call:function(){alert(this.name);} };
stu.call();
var b = new Object();
for(var a in stu)
{
    b[a] = stu[a];
}
 
stu.call = function(){alert("Test");};   //set alert value : Test
 
b.call();     //alert value : a

좋은 웹페이지 즐겨찾기