JavaScript字符串
1.字符串可以是插入到引号中的任何字符。你可以使用单引号或双引号。
2. JavaScript 字符串是原始值,可以使用字符创建: var firstName = “John”,也可以使用 new 关键字将字符串定义为一个对象: var firstName = new String(“John”)
var x = "John";
var y = new String("John");
typeof x // 返回 String
typeof y // 返回 Object
注意: 不要创建 String 对象。它会拖慢执行速度,并可能产生其他副作用。
var x = "John";
var y = new String("John");
(x === y) // 结果为 false,因为 x 是字符串,y 是对象
=== 为绝对相等,即数据类型与值都必须相等。
( 1、对于 string、number 等基础类型,== 和 === 是有区别的
a)不同类型间比较,== 之比较 "转化成同一类型后的值" 看 "值" 是否相等,=== 如果类型不同,其结果就是不等。
b)同类型比较,直接进行 "值" 比较,两者结果一样。
2、对于 Array,Object 等高级类型,== 和 === 是没有区别的
进行 “指针地址” 比较
3、基础类型与高级类型,== 和 === 是有区别的
a)对于 ==,将高级转化为基础类型,进行 "值" 比较
b)因为类型不同,=== 结果为 false
4、!= 为 == 的非运算,!== 为 === 的非运算)
var num=1;
var str="1";
var test=1;
test == num //true 相同类型 相同值
test === num //true 相同类型 相同值
test !== num //false test与num类型相同,其值也相同, 非运算肯定是false
num == str //true 把str转换为数字,检查其是否相等。
num != str //false == 的 非运算
num === str //false 类型不同,直接返回false
num !== str //true num 与 str类型不同 意味着其两者不等 非运算自然是true啦
3.字符串属性
a)constructor—返回创建字符串属性的函数
b)
prototype— 允许您向对象添加属性和方法
c)
length— 返回字符串的长度
prototype:
<html>
<body>
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
employee.prototype.salary=null;
bill.salary=20000;
document.write(bill.salary);
</script>
</body>
</html>
constructor:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<p id="demo">单击按钮创建一个Date对象,并返回构造函数。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var d = new Date();
var x=document.getElementById("demo");
x.innerHTML=d.constructor;
}
</script>
</body>
</html>
运行结果:function Date() { [native code] }
length:(leave out )
and the method of string.