zoukankan      html  css  js  c++  java
  • 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是不等的。
    

      

  • 相关阅读:
    高级软件工程第一次作业
    《高级软件工程》团队第二次作业
    《高级软件工程》结对第二次作业
    《高级软件工程》团队第一次作业
    《高级软件工程》结对第一次作业
    《高级软件工程》第二次作业
    《高级软件工程》第一次作业
    冲刺NO.1
    冲刺No.4
    冲刺No.3
  • 原文地址:https://www.cnblogs.com/fandong90/p/5537724.html
Copyright © 2011-2022 走看看