字符串属性:
这三种属性所有主流浏览器都支持
constructor
语法:string.constructor
作用1:返回字符串对象属性创建的函数
实例.
<script>
var myName="Nico";
document.write(myName.constructor);
</script>
运行结果如下:
作用2:返回所有JavaScript变量的构造函数
"Nico".constructor //function String() {[native code]}
(21).constructor //function Number() {[native code]}
true.constructor //function Boolean() {[native code]}
[1,2,3].constructor //function Array() {[native code]}
{name:"Nico",age:21}.constructor //function Object() {[native code]}
new Date().constructor //function Date() {[native code]}
function() {}.constructor //function Function() {[native code]}
例.
使用constructor属性查看对象是否为数组(是否包含字符串“Array”):
var myArray = ["Banana", "Orange", "Apple", "Mango"];
myArray.constructor.toString().indexOf("Array") > -1; //true
length
语法:string.length
作用:返回字符串的长度(字符数)
实例:
<script>
var myName="Nico";
document.write(myName.length); //4
</script>
prototype
语法:object.prototype.name=value
作用:允许我向对象添加属性和方法
注:prototype是全局属性,适用于所有的JavaScript对象
实例.
<script>
function N_name(n2,n3,n4){ //定义函数
this.n2=n2;
this.n3=n3;
this.n4=n4;
}
/* function N_name(){} */
var Nico = new N_name("haha","xixi",21); //创建对象
/* var Nico = new N_name; */
/* var Nico = new N_name(); */
N_name.prototype.salary=null;
Nico.salary=3000; //给对象增添属性
document.write(Nico.salary); //3000
</script>
注:声明新变量时,就相当于创建了新对象,可以使用new来声明类型
字符串对象方法具体讲解见下篇