<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
</head>javascript
<body>
<script type="text/javascript">html
//定義一個對象
var person = {
firstname:'john',
lastname:'doe',
age:50,
eyecolor:'blue',java
say:function (argument) {
alert(argument);
alert(this.age);
}
}函數
//alert(person.age); //訪問對象屬性
//alert(person['age']); //訪問對象屬性
//person.say('fff'); //訪問對象的訪問
//alert(person.say); //函數做爲一個屬性被訪問this
//若是變量在函數內沒有聲明(沒有使用 var 關鍵字),該變量爲全局變量。xml
var carName="volvo"; //全局變量
function myFunction()
{htm
bicyCleName="Giant"; //全局變量對象
var a='a'; //局部變量
console.log(carName);
console.log(bicyCleName);
console.log(a);
}事件
myFunction();
console.log(bicyCleName); //正常輸出
//console.log(a); //會報錯 test.html:46 Uncaught ReferenceError: a is not definedip
var b;
var c=null;
console.log(b); //undefined
console.log(c); //null
//alert(typeof e); //輸出的是undefined
</script>
<!-- 原來事件是能夠直接添加js代碼的,不用先聲明函數能夠-->
<button onclick="getElementById('demo').innerHTML=Date()">如今的時間是?</button>
<p id="demo"></p>
<button onclick="this.innerHTML=Date()">如今的時間是?</button>
<script type="text/javascript">
//字符串能夠是對象
//不要建立 String 對象。它會拖慢執行速度,並可能產生其餘反作用:
var x = "John";
var y = new String("John");
console.log(typeof x); // 返回 String
console.log(typeof y); //返回 Object
console.log((x === y)); // 結果爲 false,由於是字符串,y 是對象 === 爲絕對相等,即數據類型與值都必須相等。
console.log(x.length);
var txt='';
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + x+person[x]+'\n';
}
console.log(txt);
//你能夠設置爲 null 來清空對象:
var person = null; // 值爲 null(空), 但類型爲對象
//你能夠設置爲 undefined 來清空對象:
var person = undefined; // 值爲 undefined, 類型爲 undefined
</script></body></html>