String與Object數據類型

一:String

1、兩種創建對象的方式(小string與大string)

//第一種
var x="abc"
alert(typeof x)//string 
//第二種
var y=new String("abc")
alert(typeof y)//object

2、小string與大string的屬性與函數是相通的,常用的函數有:

indexof() :子字符串第一次出現在字符串中的索引 

lastindexof() :最後一次... 

 substr()   subsring():截取字符串

區別: 

alert('abcdef'.substr(2,4));//cdef
alert('abcdef'.substring(2,4));//cd

split() :拆分  tolowercase():轉小寫   tosuppercase():轉大寫 

二:Object

1、object類型是所有類型的超類,自定義的數據類型默認繼承object

2、object包含的常用的屬性和函數:

屬性:prototype、constructor

函數:tostring()、valueof()、tolocalstring()

prototype能夠給類動態擴展屬性和函數所以極爲重要,js中定義的類都會繼承object類的屬性和函數,也就是所自定義的類中也有prototype。

Product=function(pname,price){
	this.pname=pname;
	this.price=price;
		}
//給Product類增加一個函數
Product.prototype.getPname=function(){
	 return this.pname;
		}
var p=new Product("banana",2);
document.write(p.getPname());//banana
//給String擴展函數
String.prototype.suiyi=function(){
	alert("hah");
		}
'abc'.suiyi();//hah

另:js屬於弱類型,一個函數可當多個函數用(即是函數也是構造函數)

user=function(username,password){
	this.username=username;
	this.password=password;
		}
var u1=new uesr();
var u2=new user(password);
var u3=new nser(username,password);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章