Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript字符串对象只读?_Javascript_String_Readonly - Fatal编程技术网

Javascript字符串对象只读?

Javascript字符串对象只读?,javascript,string,readonly,Javascript,String,Readonly,这是否意味着我只能通过.split(“”然后.join(“”)将字符串用作字符数组 回答:是的,在中,Javascript字符串是只读的(也称为不可变的)。这个问题的答案如下: 这是正确的 当然,您可以构建一个函数来处理这个问题 有关这方面的不同示例,请参见本SO帖子: 字符串是,所以是的a。您还可以使用slice:a='j'+a.slice(1),或replace:a=a.replace(/^h/i,'j') 您可以创建一个自定义的可变字符串对象,例如(特别是参见方法replaceCh

这是否意味着我只能通过
.split(“”
然后
.join(“”
)将字符串用作字符数组


回答:是的,在
中,Javascript字符串是只读的
(也称为不可变的)。这个问题的答案如下:

  • 这是正确的

    当然,您可以构建一个函数来处理这个问题

    有关这方面的不同示例,请参见本SO帖子:

    字符串是,所以是的<如果要更改字符串,则应重新分配代码>a。您还可以使用
    slice
    a='j'+a.slice(1)
    ,或
    replace
    a=a.replace(/^h/i,'j')


    您可以创建一个自定义的可变
    字符串
    对象,例如(特别是参见方法
    replaceCharAt
    )。

    如果需要能够像处理数组或字符一样对字符串执行操作,您可以创建一些原型:

    a[0]==="H" //true
    a[0]="J"
    a[0]==="J" //false
    a[0]==="H" //true
    
    String.prototype.splice=函数(开始、长度、插入){
    var a=这个.slice(0,开始);
    var b=this.slice(开始+长度,this.length);
    如果(!insert){insert=”“;};
    返回新字符串(a+insert+b);
    };
    String.prototype.push=函数(插入){
    var a=这个
    返回新字符串(a+插入);
    };
    String.prototype.pop=函数(){
    返回新字符串(this.slice(0,this.length-1));
    };
    String.prototype.concat=函数(){
    如果(arguments.length>0){
    var字符串=”;
    for(var i=0;i
    回复你好, hbllo, hell,hellohbllohell,ehllo

    字符串原型的.valueOf()方法不会返回其原始值吗? 像


    基本上,javascript中的字符串有两种类型:一种是基本字符串,另一种是对象字符串。字符串对象是字符序列

    可以使用字符串的基本类型

    String.prototype.splice = function(start,length,insert) {
        var a = this.slice(0, start);
        var b = this.slice(start+length, this.length);
        if (!insert) {insert = "";};
        return new String(a + insert + b);
    };
    
    String.prototype.push = function(insert) {
        var a = this
        return new String(a + insert);
    };
    
    String.prototype.pop = function() {
        return new String(this.slice(0,this.length-1));
    };
    
    String.prototype.concat = function() {
        if (arguments.length > 0) {
            var string = "";
            for (var i=0; i < arguments.length; i++) {
                string += arguments[i];
            };
            return new String(this + string);
        };
    };
    
    String.prototype.sort = function(funct) {
        var arr = [];
        var string = "";
        for (var i=0; i < this.length; i++) {
            arr.push(this[i]);
        };
        arr.sort(funct);
        for (var i=0; i < arr.length; i++) {
            string += arr[i];
        };
        return new String(string);
    };
    
    var a = new String("hello");
    var b = a.splice(1,1,"b");
    var c = a.pop();
    var d = a.concat(b,c);
    var e = a.sort();
    
    或使用

       var x = "hello";
    
       console.log(x);
    
         output : "hello"
        x = "j"+x.substring(1);
        console.log(x);
        output : "jello";
    

    出于所有的目的,我所能看到的就是新字符串(“hello”)实际上等同于“hello”。它没有额外的数组方法等,只能通过相同的方法进行操作@PlacidCow它确实需要[]来获得单个字符。感谢您的详细回答,但我问的是Javascript字符串是否为只读,您的所有回答都使用一个新字符串来替换旧字符串。
       var x = "hello";
    
       console.log(x);
    
         output : "hello"
        x = "j"+x.substring(1);
        console.log(x);
        output : "jello";
    
      var x = new String("hello");
      console.log(x.toString());
      x = "j"+x.substring(1);