上面描述了数据类型,学习完数据类型就不得不提到变量。 一.关于Javascript变量声明 var n=999; function f1(){ alert(n); } f1(); // 999
function f1(){ var n=999; } alert(n); // error
var x='000 '; document.writeln(x); //得出'000 ' a(); function a(){ var x='aaa '; function b(){ document.writeln(x); //undefined var x='bbb '; document.writeln(x); //bbb } b(); document.writeln(x); //aaa } //结果是:000 undefined bbb aaa
var x = "global"; function f() { var x='f1'; function f2(){ x='f2' ;//这里我有些混淆,GLOBAL的X应该重新被赋值为'f2' alert(x); //返回"f2" alert(window.x); //返回"global" } f2(); alert(x) //返回"f2" } f(); alert(x); //返回"global",没有被重新赋值为:f2 //结果分别弹出:f2 global f2 global
var name = 'Jeffrey'; var lastName = 'Way'; function doSomething() {...} console.log(name); // Jeffrey -- or window.name
var DudeNameSpace = { name : 'Jeffrey', lastName : 'Way', doSomething : function() {...} } console.log(DudeNameSpace.name); // Jeffrey
var someItem = 'some string'; var anotherItem = 'another string'; var oneMoreItem = 'one more string';
var someItem = 'some string', anotherItem = 'another string', oneMoreItem = 'one more string';
|