为什么toString不是JavaScript中的通用函数

Jyo*_*may 5 javascript oop inheritance

我正在尝试做这样的事情。

 var myFunc = function() {}
 myFunc.prototype = new String();
 myFunc.prototype.replace = function() {return 'hii, Mr '+ this.toString();}       

 var oVal = new myFunc('Jyotirmay');
 oVal.replace();
Run Code Online (Sandbox Code Playgroud)

o / p ::未捕获的TypeError:String.prototype.toString不是通用的(...)

为什么通常会出现“非通用功能”错误?

更清楚地说,我如何将我的参数(即Jyotirmay)从继承的类传递到基类(即字符串)。这样我就可以通过调用任何适当的字符串函数来获取该值。

我不想通过处理函数中的变量来获取传递的值。我希望由父类处理。您可以用其他语言说出super()。

Xot*_*750 2

目前尚不清楚您到底想从您的问题和评论中实现什么目的,但这也许就是您想要做的全部?

function myFunc(inputArg) {
    this.inputArg = inputArg;
}

myFunc.prototype = {
    replace: function () {
        return 'hii, Mr ' + this.inputArg;
    },
    
    toString: function () {
        return '' + this.inputArg;
    }
};

myFunc.prototype.valueOf = myFunc.prototype.toString;

function log(inputArg) {
    document.getElementById('out').appendChild(document.createTextNode(inputArg + '\n'));
}

var oVal = new myFunc('Jyotirmay');

log(oVal);
log(oVal.replace());
Run Code Online (Sandbox Code Playgroud)
<pre id="out"></pre>
Run Code Online (Sandbox Code Playgroud)

至于Why is toString not generic,这是因为并不是所有的对象都可以通过相同的转换方法表示为字符串。

根据您的最新评论进行更新

众所周知,在 Javascript 中子类化本机对象即使不是不可能,也是非常困难的。有一些技巧可以让你取得部分成功,但我不会推荐它们,祝你在不同的环境中好运。

两个(但不是唯一的)这样的黑客是:

偷窃自iframe

function stealObject(objectName, myVariableName) {
    var iframe = document.createElement('iframe');

    iframe.style.display = 'none';
    iframe.src = 'javascript:parent.' + myVariableName + ' = ' + objectName;
    document.body.appendChild(iframe);
    document.body.removeChild(iframe);

    return window[myVariableName];
}

function log(inputArg) {
    document.getElementById('out').appendChild(document.createTextNode(inputArg + '\n'));
}

try {
    stealObject('String', 'MyString');
    MyString.prototype.replace = function () {
        return 'hii, Mr ' + this;
    };


    var oVal = new MyString('Jyotirmay');

    log(oVal);
    log(oVal.toUpperCase());
    log(oVal.replace());
} catch (e) {
    log(e);
}
Run Code Online (Sandbox Code Playgroud)
<pre id="out"></pre>
Run Code Online (Sandbox Code Playgroud)

在 SO 片段中不起作用,因为SecurityError: Sandbox access violation:但可以在此jsFiddle上看到它。typeof oVal将返回object,不返回string,并将oVal instanceof String返回falseoVal.constructor === String将返回false

另一个黑客

function MyString() {
    this.str = '' + arguments[0];
};

with(MyString.prototype = new String()) {
    toString = valueOf = function () {
        return this.str;
    };
}

MyString.prototype.replace = function () {
    return 'hii, Mr ' + this;
};

function log(inputArg) {
    document.getElementById('out').appendChild(document.createTextNode(inputArg + '\n'));
}

var oVal = new MyString('Jyotirmay');

log(oVal);
log(oVal.toUpperCase());
log(oVal.replace());
Run Code Online (Sandbox Code Playgroud)
<pre id="out"></pre>
Run Code Online (Sandbox Code Playgroud)

这个魔法length属性被破坏了,你需要打电话oVal.toString().length来代替。typeof oVal将返回object,但string不会oVal instanceof String返回trueoVal.constructor === String将返回true