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);

 

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