码迷,mamicode.com
首页 > 编程语言 > 详细

javaScript 工作必知(三) String .的方法从何而来?

时间:2016-05-28 17:25:23      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

String

                我们知道javascript 包括:number,string,boolean,null,undefined 基本类型和Object 类型。

                       在我的认知中,方法属性应该是对象才可以具有的。

                

 var  str="hello,world";
 var  s=str.subString(1,4);//ell
 alert(typeof(str)+":"+typeof(s));//string:string

             从上面的返回类型来看,str是string 类型的。

       再看下面的 如何使用全局对象声明一个字符串。

 

var c=new String(str);
alert(typeof(c));//Object
alert(c.toString());//hello,world

            那我能不能认为: 当我处理字符串的时候,

                javascript编译器先把str字符串,使用new String(str);成了对象。然后在调用其处理办法,然后使用toString()方法返回个字符串呢。

 

临时对象的创建和销毁

          从上面的实例我知道javascript在处理字符串、number,boolean 时就会创建临时对象,然后销毁。

   var a = "hello,world";
        var c = new String(a); //创建了string 对象。
        c.len = 4;
        alert(typeof (c));//object;
        alert(c.len);//4

///////////////////////////////////////////////////////////////////////
         a.len=5;
         alert(a.len);//undefined

  a.len 编译器没有报错,是因为创建的临时对象操作完后,又销毁了。

            

==和===

     

a==c ;//true;
a===c;//false; 字符串和object是不等的。

  

javaScript 工作必知(三) String .的方法从何而来?

标签:

原文地址:http://www.cnblogs.com/fandong90/p/5537724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!