这段代码:
'use strict'
function Ob() {}
Ob.prototype.add = () => {
this.inc()
}
Ob.prototype.inc = () => {
console.log(' Inc called ');
}
module.exports = new Ob();
Run Code Online (Sandbox Code Playgroud)
由此代码使用:
'use strict'
const ob = require('./ob')
ob.add();
Run Code Online (Sandbox Code Playgroud)
调用梯形图时,我收到此错误:
this.inc is not a function
当我将第一个代码段更改为:
'use strict'
function Ob() {}
Ob.prototype.add = function() {
this.inc();
}
Ob.prototype.inc = function() {
console.log(' Inc called ');
}
module.exports = new Ob();
Run Code Online (Sandbox Code Playgroud)
一切都很好,我得到:
Inc called
为什么第一个版本会抛出?
更新:如何使用箭头功能使其工作?
我当时在React.js应用程序上工作,最初使用的是箭头功能,但出于好奇,我决定尝试正常功能,但正常功能无效。我认为他们都应该输出相同的内容,这是怎么回事?
handleChange = event => this.setState({init: event.target.value})
handleChange(event){
this.setState({init: event.target.value})
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个函数:
这是我的解决方案,目前在某些方面有效:
function getOddLengthWordsAtProperty(obj, key) {
var output = [];
for(var i = 0; i < obj.key.length; i++){
if (key in obj){
if (Array.isArray(obj[key])){
if(obj[key].length){
if(obj.key[i].split("").length % 2 !== 0){
output.push(obj.key[i]);
}
}
}
}else{
return [];
}
}
return output;
}
var obj = {
key: ['It', 'has', 'some', 'words']
};
var output = getOddLengthWordsAtProperty(obj, 'key');
console.log(output); // --> ['has', 'words']
Run Code Online (Sandbox Code Playgroud)
这里的问题是我的代码返回:
TypeError:无法读取未定义的属性"length"
即使这是" 事件处理程序不处理动态内容 " 的重复问题,即使按照那里给出的答案,我仍然遇到同样的问题.请帮忙 ....
userinfo.topics.forEach(ele=>{
$("#topics_subscribed").append(`<li class="list-group-item col-4">${ele} <i style="color:red;font-size:.7em;" class="removeitem fa fa-times"/></li>`) ;
});
$(document).on('click', '.removeitem', ()=> {
console.log('clicked') ;
$(this).parent().remove() ;
});
Run Code Online (Sandbox Code Playgroud)
请在将此标记为重复之前回答其原因.如何解决这个问题?(我会在答复后立即删除这个问题....)
我将Array在我的Node.JS服务器应用程序中选择一个随机元素.我也使用ES6的箭头语法.在对象的原型中,我添加了一个名为的函数random().您可以在下面找到我的代码:
Array.prototype.random = () => { // --> add element to prototype of `Array` object
let rnd = Math.random(); // --> gives me a random number between 0 and 1
let len = this.length; // --> `this.length` gives `undefined`
let naam = rnd * len - 1; // --> result is `NaN`
let numb = Math.floor(naam); // --> `numb` is also `NaN`
let arr = this; // --> `arr` contains an `Object` with none
// properties …Run Code Online (Sandbox Code Playgroud)