首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

javascript 权威指南 学习札记1

2012-11-23 
javascript 权威指南 学习笔记1var scope global// Declare a global variablefunction checkscope()

javascript 权威指南 学习笔记1

var scope = "global"; // Declare a global variablefunction checkscope( ) { var scope = "local"; // Declare a local variable with the same name document.write(scope); // Use the local variable, not the global one}checkscope( ); // Prints "local" ?

scope = "global"; // Declare a global variable, even without varfunction checkscope( ) { scope = "local"; // Oops! We just changed the global variable document.write(scope); // Uses the global variable myscope = "local"; // This implicitly declares a new global variable document.write(myscope); // Uses the new global variable}checkscope( ); // Prints "locallocal"document.write(scope); // This prints "local"document.write(myscope); // This prints "local" ?

var scope = "global scope"; // A global variablefunction checkscope( ) { var scope = "local scope"; // A local variable function nested( ) { var scope = "nested scope"; // A nested scope of local variables document.write(scope); // Prints "nested scope" } nested( );}checkscope( ); ?

Note that unlike C, C++, and Java, JavaScript does not have block-level scope. All variables declared in a function, no matter where they are declared, are defined function test(o) { var i = 0; // i is defined throughout function if (typeof o == "object") { var j = 0; // j is defined everywhere, not just block for(var k = 0; k < 10; k++) { // k is defined everywhere, not just loop document.write(k); } document.write(k); // k is still defined: prints 10 } document.write(j); // j is defined, but may not be initialized} ?

var scope = "global";function f( ) { alert(scope); // Displays "undefined", not "global" var scope = "local"; // Variable initialized here, but defined everywhere alert(scope); // Displays "local"}f( ); ?

function f( ) { var scope; // Local variable is declared at the start of the function alert(scope); // It exists here, but still has "undefined" value scope = "local"; // Now we initialize it and give it a value alert(scope); // And here it has a value} ?

var x; // Declare an unassigned variable. Its value is undefined.alert(u); // Using an undeclared variable causes an error.u = 3; // Assigning a value to an undeclared variable creates the variable.

var a = 3.14; // Declare and initialize a variablevar b = a; // Copy the variable's value to a new variablea = 4; // Modify the value of the original variablealert(b) // Displays 3.14; the copy has not changed?

var a = [1,2,3]; // Initialize a variable to refer to an arrayvar b = a; // Copy that reference into a new variablea[0] = 99; // Modify the array using the original referencealert(b); // Display the changed array [99,2,3] using the new reference?

?

?

var s = "hello"; // Allocate memory for a stringvar u = s.toUpperCase( ); // Create a new strings = u; // Overwrite reference to original string?

?

热点排行
Bad Request.