给字符串新增方法实现功能

        给字符串对象定义一个addPrefix函数,当传入一个字符串str时,他会返回新的带有指定前缀的字符串。比如console.log("world".addPrefix("hello"));//"helloworld"

<script>
    String.prototype.addPrefix = function(str){
        return str + this;
    }
    console.log('world'.addPrefix('hello'));//helloworld
</script>

猜你喜欢

转载自blog.csdn.net/m0_73460278/article/details/126987705