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


    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是不等的。
    

      

  • 相关阅读:
    Eclipse 读取config目录下文件
    cakephp 中Console / Shell 有什么优点?
    cakephp中使用 find('count')方法
    [转]using components in Cakephp 2+ Shell
    [转]Git for windows 下vim解决中文乱码的有关问题
    在Foxmail中添加阿里云企业邮箱账号
    Cakephp在Controller中显示sql语句
    java线程的基本概念
    mysql varchar到底能存多少字符。
    mysql 联合索引匹配原则
  • 原文地址:https://www.cnblogs.com/fandong90/p/5537724.html
Copyright © 2020-2023  润新知