2017-02-20 27 views
1

这是一个JavaScript函数:如何访问参数中的JavaScript功能

String.prototype.digit = function() { 
    console.log(this); // 'this' contain the object 
    return false; 
}; 

如何访问参数'14'的功能,而调用函数是这样的:

'14'.digit(); 
+0

供参考:一个适当的函数调用会像'函数(ARG){的console.log(ARG); }' 关键字'this'不是参数,而是您从中调用函数的对象。 – getjackx

回答

5

你可以使用Object#valueOf方法

The valueOf()方法返回指定对象的原始值。

String.prototype.digit = function() { 
 
    console.log(this.valueOf()); 
 
    return false; 
 
}; 
 

 
'14'.digit();

Object#toString或方法。

toString()方法返回表示对象的字符串。

String.prototype.digit = function() { 
 
    console.log(this.toString()); 
 
    return false; 
 
}; 
 

 
'14'.digit();

+0

谢谢...... !!!工作:) –