简体   繁体   English

方法toString是未定义的JavaScript

[英]Method toString is undefined JavaScript

I'm experimenting with key-value pair of JS objects. 我正在尝试JS对象的键值对。

<div id="parent" class="parent">
    <div id="child" class="child">
        <input type="text" class="text"/>
        <input id="submit" type="submit" onclick="doThis()"/>
    </div>
<div>

and corresponding JS code: 和相应的JS代码:

function doThis(){
    var span= document.createElement("span");
    var parent=document.getElementById("parent");
    var child=document.getElementById("child");
    var submit=document.getElementById("submit");
    child.insertBefore(span,submit);
    myKeys=[];
    for(var key in submit){
        myKeys.push("{"+key);
        myKeys.push(" "+submit.key + "}");
    }  
    span.innerHTML=myKeys;
}

It works correctly. 它可以正常工作。 But if we replace submit.key to submit.key.toString() it doesn't work. 但是,如果我们将submit.key替换为submit.key.toString()它将无法正常工作。 JSFIDDLE . JSFIDDLE I dont understand why the error's description is Uncaught TypeError: Cannot call method 'toString' of undefined. 我不明白为什么错误的描述是Uncaught TypeError: Cannot call method 'toString' of undefined. toString method defined for all JS object. 为所有JS对象定义的toString方法。

submit.key will be undefined since you meant submit[key]. 由于您的意思是submit [key],所以未定义submit.key。 Object accessors are required since you want to use the variable to lookup the property. 因为您要使用变量来查找属性,所以需要对象访问器。

On checking what submit holds, I got 在检查submit ,我得到了

console.log(submit) 

returns 退货

<input id="submit" type="submit" onclick="doThis()"/>

Whereas, 鉴于,

console.log(submit.key) //returns undefined

because submit does not hold key 因为submit不持有key

Hence the error, toString() method cannot call of undefined 因此,错误toString() method cannot call of undefined

The problem you are facing is because of: 您面临的问题是由于:

submit.key

you need to use: 您需要使用:

submit[key]

Not only that, but calling toString() on this will fail if the key is null or undefined . 不仅如此,如果键为nullundefined ,对此调用toString()也会失败。 So, better try-catch it and then review the log. 因此,最好尝试捕获它,然后查看日志。

See your updated fiddle: http://jsfiddle.net/abhitalks/BYwz9/4/ 查看更新的小提琴: http : //jsfiddle.net/abhitalks/BYwz9/4/

Hope that helps. 希望能有所帮助。

Update : ( regarding the syntax of object key bracket notation ) 更新 :( 关于对象键括号符号的语法

From this reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects 从此参考资料: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. 对象属性名称可以是任何有效的JavaScript字符串,也可以是任何可以转换为字符串的内容,包括空字符串。 However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. 但是,任何不是有效JavaScript标识符的属性名称(例如,具有空格或连字符或以数字开头的属性名称)都只能使用方括号表示法进行访问。 This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime). 当要动态确定属性名称时(当直到运行时才确定属性名称时),该符号也非常有用。

In order to check the difference it makes in your use-case, please change the fiddle to both notations and check the console log. 为了检查它与您的用例之间的差异,请将小提琴更改为两种表示法,然后检查控制台日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM