JavaScript--String对象的三个属性

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS的三个属性</title>
<script type="text/javascript" src="tools.js"></script>
</head>
<body>
	<script type="text/javascript">
		var str = "wwee123";
		//1.length--字符串长度
		println(str.length);//7
		
		
		//2.constructor--相当于Java中的instanceof()
		println(str.constructor);//function String() { [native code] }
		println(str.constructor == String);//true
		
		//3.prototype--相当重要!!用它可以向对象添加属性和方法(所有对象都有)
		//添加属性
		String.prototype.age = 19;
		println("age:"+"aa".age);//age:19
		//添加方法
		String.prototype.toCharArray = function(){
			var res = [];
			for(var i = 0;i<this.length;i++){
				res[i] = this.charAt(i);
			}
			return res;
		}
		println(str + " -> "+str.toCharArray());//wwee123 -> w,w,e,e,1,2,3
	</script>
</body>
</html>

function print(e){
	document.write(e);
}

function println(e){
	document.write(e+"<br>");
}

猜你喜欢

转载自blog.csdn.net/qq_38238041/article/details/80717518