編程世界裏的兩種元素:數據和代碼;
Javascript中的簡單數據:undefined,null,boolean,number,string;全都是小寫的;
Javascript中的內置函數:Number,String,Object,Function;javascript區分大小寫;
Typeof返回返回表明的類型;
完全有數組組成的字符串與該字符串表示的的值是相等的;比如”123”=123;註意:”0123”==123的值是false;因為javascript將0開頭的整數常量以8進制處理,所以”0123”是8進制,”123”是10進制;
Object就是對象的類,在javascript中不管是復雜的數據和代碼,都可以組成object形式的對象;
<script
type="text/javascript">
var
life={};
for(life.age=1;life.age<3;life.age++){
switch(life.age){
case
1:
life.body="卵細胞";
life.say=function(){alert(this.age+this.body)};
break;
case
2:
life.tail="尾巴";
life.gill="腮";
life.body="蝌蚪";
life.say=function(){alert(this.age+this.body+this.tail+this.tail)};
break;
case
3:
delete
life.gill;
delete
life.tail;
life.legs="四條腿";
life.lung="肺";
life.body="青蛙";
life.say=function(){alert(this.age+this.body+this.legs+this.lung)};
break;
};
life.say();
}
</script>
Javascript的內置函數;
函數的寫法分為:定義式和變量式;
<script
type=”text/javascript”>
Var
myfun=function(){
Alert(“妳好”);
};
Myfun();
Myfun=function(){
Alert(“yeah”);
};
Myfun();
</script>
此為,變量式;第壹次調用函數後,函數變量又被賦予了新的函數體,所以輸出不同;
<script
type=”text/javascript”>
Function
myfun(){
Alert(“hello”);
};
Mufun();
Function
myfun(){
Alert(“huhu”);
};
Myfun();
</script>
函數簽名相同的函數;因為兩個函數簽名相同,所以後壹個把前壹個的內容輸出覆蓋了,所以只會輸出對後壹個的內容;
Javascript的作用域:
在javacript裏的全局環境就是壹個對象,這個對象是javascrit運行環境的跟,對於瀏覽器中的javascript來說,這個跟對象就是window對象,對於全局的
javascript語句來說,window對象就相當與當前的作用域;
Var
myname=”leadzen”;
就是定義了window作用域的壹個變量myname;
Nyname=”leaden”;
定義了window對象的壹個屬性myname;
<script
type="text/javascript">
var
youname="sunyuan";
myname="zh";
alert(myname+"like"+youname);//輸出zh like
sunyuan;
change();
function
change(){
alert("you old name
is"+youname); //you old name is undefined;
alert("my name
is"+myname);//my name is zh;
var
youname="susu";
myname="kiki";
alert(myname+"like"+youname);//kiki like
susu;
};
alert(myname+"like"+youname);//kiki like
sunyuan
</script>
了解caller屬性的用法;
<script
type="text/javascript">
function
whocallme(){
alert("my caller
is"+whocallme.caller);//輸出自己的caller;
};
function
callera(){whocallme();};
function
callerb(){whocallme();};
alert(whocallme.caller);//輸出null;
whocallme();//輸出mycallme
is null;
callera();//輸出mycallme
is function callera(){whocallme();};
callerb();//輸出mycallme
is function callera(){whocallme();};
</script>
如果函數的caller屬性是null,表示函數沒有被調用或者是被全局代碼調用,函數的caller屬性值實際是動態變化的,函數的初始caller值都是null,當調用壹個函數時,如果代碼已經運行在某個函數體內,javascript執行將會被caller屬性設置為當前函數,在推出被調用的作用域時,被調用的caller屬性又會被恢復為null值;
Javascript中只有object和function兩種東西才又對象化能力;
<script
type="text/javascript">
function
sing(){
alert(sing.author+":"+sing.poem);
};
sing.author="李白";
sing.poem="天使的翅膀是愛做的,孫媛的心是用關懷呵護的";
sing();
sing.author="李站";
sing.poem="能力不是壹朝壹夕就能變強的,需要時間,需要積累";
sing();
</script>
Sing()函數定義後,又給sing()函數動態的增加author和poem屬性;
對於對象:
<script
type="text/javascript">
var
anobject={};//壹個對象;
anobject.aproperty="property of
object";//對象的壹個屬性;
anobject.amethod=function(){alert("method of
object")};//對象的壹個方法;
alert(anobject["aproperty"]);//可以將對象當數組以屬性名作為下標來訪問屬性;
anobject["amethod"];//以對象當數組以方法名作為下標來調用方法;
for(var s in
anobject){//遍歷對象的所有屬性和方法進行叠代處理;
alert(s+"is
a"+typeof(anobject[s]));
}
</script>
對於函數:
<script
type="text/javascript">
var
anobject=function(){};//壹個函數
anobject.aproperty="property of
object";//函數的壹個屬性;
anobject.amethod=function(){alert("method of
object")};//函數的壹個方法;
alert(anobject["aproperty"]);//可以將函數當數組以屬性名作為下標來訪問屬性;
anobject["amethod"];//以函數當數組以方法名作為下標來調用方法;
for(var s in
anobject){//遍歷函數的所有屬性和方法進行叠代處理;
alert(s+"is
a"+typeof(anobject[s]));
}
</script>
Javascript中的this用法:
<script
type="text/javascript">
function
whoami(){//定義壹個函數;
alert("i am
"+this.name+"of"+typeof(this));
};
whoami();//this是根對象window,name屬性為空,輸出:i am of
object;
var
billgates={name:"bill gates"};
billgates.whoami=whoami;//將函數whoami作為billgates的方法;
billgates.whoami();//輸出i
am billgates of object;
var
stevejobs={name:"steve jobs"};
stevejobs.whoami=whoami;
stevejobs.whoami();
whoami.call(billgates);
whoami.call(stevejobs);
whoami.whoami=whoami;
//將whoami設置為自身的方法;
whoami.name="whoami";//此時this是whoami自己;
whoami.whoami();
({name:"nobody",whoami:whoami}).whoami();//創建壹個匿名對象並調用方法,輸出:i
am nobody of object;
</script>
Json數據 javascript
object natation javascript對象表示法
<script
type="text/javascript">
var
person={
name:"sunyuanyuan",
product:"softname",
chairman:{name:"shagua",age:90},
employees:[{name:"huhu",age:89},{name:"asd",age:67}],
readme:function(){return
(this.name+"product"+this.product);}
}
alert(person.name);
alert(person.product);
alert(person.chairman.name);
alert(person.chairman.age);
alert(person.employees[0].name);
alert(person.employees[0].age);
alert(person.employees[1].name);
alert(person.employees[1].age);
alert(person.readme());
</script>
註意:這裏面的readme函數是有返回值的,就在彈出框倆面顯示調用內容;
<script
type="text/javascript">
var
person={
name:"sunyuanyuan",
product:"softname",
chairman:{name:"shagua",age:90},
employees:[{name:"huhu",age:89},{name:"asd",age:67}],
readme:function(){document.write
(this.name+"product"+this.product);}
}
alert(person.name);
alert(person.product);
alert(person.chairman.name);
alert(person.chairman.age);
alert(person.employees[0].name);
alert(person.employees[0].age);
alert(person.employees[1].name);
alert(person.employees[1].age);
alert(person.readme());
</script>
註意:彈出框裏面的東西是undefined;會在頁面顯示調用內容;
Javascript裏面的構造對象:
在javascript裏面可以用new操作符結合壹個函數的形式來創建對象,
Function
myfun(){};
Var an=new
myfun();
Var an1=new
myfun();
等價於:
Function
myfun(){};
Var
an={};
Myfun.call(an);
Javascript裏面的構造函數;
<script
type="text/javascript">
function
person(name){//帶參數的構造函數;
this.name=name;//定義並初始化name屬性;
this.sayhello=function(){//定義對象方法sayhello();
alert("hello i am
"+this.name);
};
};
function
emp(name.salary){//在構造函數;
person.call(this.name);//調用父類構造函數;
this.salary=salary;//添加屬性;
this.showm=function(){
alert(this.name+"$"+this.salary);//添加對象方法;
};
};
var aa=new
person("sunayun");//創建person類的aa對象;
var bb=new
showm("sinsi",1233);//創建showm類的bb對象;
aa.sayhello();//i am,
sunayun
bb.sayhello(); //i am
sinsi
bb.showm();//sinsi $
1233
alert(aa.constructor==person);//ture
alert(bb.constructor==emp);//true;
alert(aa.sayhello==bb.sayhello);//false
</script>
Javascript中的原型(prototype)
<script
type="text/javascript">
function
person(name){
this.name=name;
//設置對象屬性,每個對象各自有壹份屬性數據;
};
person.prototype.sayhello=function(){//給person函數的prototype添加sayhello方法;
alert("hello i
am"+this.name);
}
var aa=new
person("asdfsf");//創建aa對象;
var bb=new
person("sdsd8999");//創建bb對象;
aa.sayhello();//通過對象直接調用方法;
bb.sayhello();
alert(aa.sayhello==bb.sayhello);
</script>
<script
type="text/javascript">
function
person(name){//基類構造函數;
this.name=name;
};
person.prototype.sayhello=function(){//給基類構造函數的prototype添加方法;
alert("hello i am
"+this.name);
};
function
emp(name.salary){//子類構造函數;
person.call(this.name);//調用基類的構造函數
this.salary=salary;
};
emp.prototype=new
person();//建壹個基類對象作為子類原型的原型(原型繼承)
emp.prototype.showm=function(){//給子類prototype添加方法;
alert(this.name+"$"+this.salary);
};
var aa=new
person("sdsf");//通過對象調用prototype的方法;
var bb=new
emp("23a",232);
aa.sayhello();
bb.sayhello();
bb.showm();
alert(aa.sayhello==bb.sayhello);
</script>
私有變量:
<script
type="text/javascript">
function
person(firstname,lastname,age){
//私有變量;
var_firstname=firstname;
var_lastname=lastname;
//***有變量;
this.age=age;
//方法;
this.getname=function(){
return (firstname+"
"+lastname);
};
this.sayhello=function(){
alert("hello i am
"+firstname+" "+lastname);
};
};
var aa=new
person("bill","tee",23);
var bb=new
person("sdd","ed",34);
aa.sayhello();
bb.sayhello();
alert(aa.getname()+"
"+aa.age);
alert(aa._firstname);//不能訪問私有變量;unfined;
</script>
每隔多少秒調用壹次函數的方法:
setInterval(函數的方法名,1000);
其實看完javascript的東西,覺得他和java很像,也具有和java很像的東西:繼承,封裝,多態;