我这样写我的函数: truncate('Hello world!, 5);但我想这样写我的函数: 'Hello world!'.truncate(5);function truncate(str, num) {  if (str.length <= num) {  return str}return str.slice(0, num) + '...'}console.log(truncate('Hello world!', 5))
                    
                    
                1 回答
 
                    
                    
                            慕慕森
                            
                                
                            
                        
                        
                                                
                    TA贡献1856条经验 获得超17个赞
使用原型对象来扩展方法。
String.prototype.truncate = function(num){
if (this.toString().length <= num) return this.toString();
return this.toString().slice(0,num)
};
'Hello World!'.truncate(5); // Hello
添加回答
举报
0/150
	提交
		取消
	