1.js string坑爹的“==”
js的String類型與java的String類型不同,比較的時候不用equals,可以直接用"==".
測試了下,這個"=="好像比較坑爹
<script type="text/Javascript"> var a=new String("hehe"); var b=new String("hehe"); var c="hehe"; alert("a==c: "+(a==c));//true alert("b==c: "+(b==c));//true alert("a==b: "+(a==b));//false alert("hehe"=="hehe");//true</script>
怎么能有這么沒道理的事情?a=c,b=c,但是a竟然不等于b!!!
我的結(jié)論是js中的String類型雖然沒有equals方法,但是當String類型對象與另一個String類型的變量相比較的時候,使用的方法與java的equals方法相類似。而當兩個String類型相比較的時候,"=="才與java中的"=="相類似,即比較兩個對象是否是同一個對象。
與之相對應(yīng)的java代碼,則沒有這種讓人糾結(jié)的地方
String a=new String("hehe"); String b=new String("hehe"); String c="hehe"; String d="hehe"; System.out.java中通過new創(chuàng)建的String對象用"=="相比較的時候,比較的是對象的地址值,而js中,只有當兩個對象都是用new創(chuàng)建的時候,才比較地址值。
2.js中的數(shù)組與java中的List異同點:
相同點:
1.添加的元素類型可以不同。(java使用泛型后,只能添加規(guī)定的元素類型)
2.長度可變
不同點:
js中可以訪問超過數(shù)組長度的角標,返回undefined,java中,list.get(index) index的值超過數(shù)組長度的話,運行時有異常:java.lang.IndexOutOfBoundsException
ArrayList list=new ArrayList(); list.add("re"); list.add(2); System.out.println(list.get(2));//java.lang.IndexOutOfBoundsException var t=[0,"a"]; document.write(t[5]);//undefined
3.js中的function
//js的函數(shù)可以看做一個類,它的屬性分為://實例屬性:使用this作為前綴//類屬性:使用函數(shù)名作為前綴//局部變量:直接賦值/*與java不同的是,js的類屬性只能通過類來訪問,不能夠用對象來訪問*在同一個類中,可以有同名的類屬性和實例屬性;*/var Person=function(name,age){ var me="memeda"; this.name=name; this.age=age; Person.gender="female"; this.gender="male"; Person.t="hehe";}var p=new Person("Lucy","32");with(document){ write(p.name+"<br/>");//lucy write(p.age+"<br/>");//32 write(p.gender+"<br/>");//male write(Person.gender+"<br/>");//female write(p.t+"<br/>");//undefined write(p.me+"<br/>");//undefined}js對象的屬性可以隨意添加上面的p對象并沒有t、me屬性,添加后:
p.me="ok";p.t="ttok";document.write(p.t+"<br/>");//ttokdocument.write(p.me+"<br/>");//ok
4.js中函數(shù)是獨立的,它不從屬于任何類或者對象;
雖然js中的函數(shù)可以定義在某個類中,但它可以被其他類的對象調(diào)用;而java中的方法是實例方法或者是靜態(tài)方法(類方法),是屬于對象或者類的。call()方法作用:使得不同的對象能夠調(diào)用某個方法。
//call的語法格式:函數(shù)名.call(調(diào)用對象,參數(shù)1,參數(shù)2···);沒有參數(shù)的話可以省略 var wolf=function(name,voice,action){ this.name=name; this.voice=voice; this.action=action; this.info=function(){ document.write(this.name+"的叫聲是"+this.voice+"吃食物的方式是:"+this.action+"<br/>"); } } new wolf("wolf","ao~","撕咬").info();//wolf的叫聲是ao~吃食物的方式是:撕咬 var cat=function(name,voice,action) { this.name=name; this.voice=voice; this.action=action; } new wolf("wolf","ao~","撕咬").info.call(new cat("貓","miao~","賣萌"));//貓的叫聲是miao~吃食物的方式是:賣萌5.js函數(shù)的參數(shù)傳遞與java方法的參數(shù)傳遞一樣:都是值傳遞
var Person=function() { this.name="wang"; this.age="24"; } var changeP=function(person) { person.age="1"; person.name="cheng"; person=null; } var p=new Person(); document.write("改變前,p.name="+p.name+",p.age="+p.age+"<br/>");// 改變前,p.name=wang,p.age=24 changeP(p); document.write("改變后,p.name="+p.name+",p.age="+p.age+"<br/>");//改變后,p.name=cheng,p.age=1 document.write(p==null);//false上面的例子中,changeP()中傳入的參數(shù)是一個對象,調(diào)用這個方法的時候,實際傳入的不是對象本身,而是一個對象地址值的副本。因為這個地址值與原來對象的地址值一樣,所以能夠修改對象的屬性;而在p=null;這里,是切斷了副本的引用,對原來的對象沒有影響。在java中,得到的運算結(jié)果也是一樣的。
class Person{ String name; String age; public Person(String name,String age) { this.name=name; this.age=age; } public String toString() { return "Person["+this.name+","+this.age+"]"; }}public class Test{ public static void changeP(Person p) { p.age="0"; p.name="cheng"; p=null; } public static void main(String[] args) { Person p=new Person("wang","24"); System.out.println("before "+p);//before Person[wang,24] changeP(p); System.out.println("after "+p);//after Person[cheng,0] }}6.js有參數(shù)的函數(shù)在調(diào)用的時候,可以不傳入?yún)?shù);java有參數(shù)的方法必須傳入?yún)?shù);函數(shù)名是js中函數(shù)的唯一標識,js中沒有java中重載的概念。
var method=function(t) { alert(t); } method();//undefined如果在js代碼中先后定義了兩個同名的函數(shù),即使函數(shù)傳入的參數(shù)不同,后面的函數(shù)也會覆蓋前面的。
var method1=function() { alert("沒有參數(shù)的構(gòu)造器"); } var method1=function(t) { alert("有參數(shù)的構(gòu)造器"); } method1();//"有參數(shù)的構(gòu)造器"作為一種弱類型語言,js中需要參數(shù)的函數(shù),一般都要先判斷傳入的參數(shù)是否符合要求
var changeName=function(p,newname) { if(typeof p=='object'&& typeof p.name=='string' && typeof newname=='string') {p.name=newname; document.write(p.name+"<br/>");} else{ document.write("傳入的參數(shù)類型不符合要求 "+typeof(p)+" "+typeof(newname)+"<br/>"); } } changeName();// 傳入的參數(shù)類型不符合要求 undefined undefined changeName("a","s");// 傳入的參數(shù)類型不符合要求 string string person={name:"wang"}; changeName(person,"zhang");// zhang7.對象的屬性
js中調(diào)用某個對象的屬性可以用objName.propertyName的方式,調(diào)用類屬性的方式是類名+屬性名;
與java不同的是,js的屬性可以為函數(shù);在某些情況下,調(diào)用js屬性必須使用objName[propertyName]的方式
var Person=function(name,age){ this.name=name; this.age=age; this.show=function(){ document.write("此人年齡: "+this.age+"此人姓名: "+this.name+"<br/>"); }}var p=new Person("wang","24");for(var property in p){ // 這里必須使用p[property]的形式來調(diào)用對象屬性,因為如果使用p.property的方式,js會在p對象里尋找名為property的屬性,得到undefined document.write(property+"屬性的值是"+p[property]+"<br/>"); // 運行結(jié)果// name屬性的值是wang// age屬性的值是24// show屬性的值是function (){ document.write("此人年齡: "+this.age+"此人姓名: "+this.name+" "); }}8.js對象、類的屬性和方法都可以任意添加
為某個類添加屬性的方式是:類名.prototype.屬性名=sth;
為某個類添加方法的方式是:類名.prototype.方法名=function(){};
應(yīng)該盡量避免在某個類中直接定義方法,因為這可能造成內(nèi)存泄漏和閉包,prototype的方式讓程序更安全,并提高了性能;
如果在類中直接定義一個方法,那么每創(chuàng)建一個該類的新對象,在同時也會創(chuàng)建一個內(nèi)置方法的對象,容易造成內(nèi)存泄漏;
通過prototype屬性為類添加方法,讓所有實例對象都能夠共享該方法
var Person=function(){ var s="secret"; this.info=function(){ return s; }}var p=new Person();var s=p.info();alert(s);//secretPerson.prototype.test=function(){ alert("p的新方法");}p.test();
新聞熱點
疑難解答