javascript - 遍历原型(prototype)中的对象

标签 javascript prototype

我知道如何在 javascript 中循环烘烤对象 I。 但是,当我尝试在对象的原型(prototype)中执行此操作时,出现“未定义不是函数”错误。我的代码如下

var createObject = function(strIn) {
  this.result = strIn;
}

createObject.prototype.toObject = function() {
  var objData = this.result.split('&');
  this.result = Object.create(null); //{}
  var res = Object.create(null);
  objData.forEach(function(value, index) {
    var test = value.split('=')
    return res[test[0]] = test[1];
  });
  this.result = res;
  res = Object.create(null);
  return this.result;

}

createObject.prototype.toString = function() {
  //{ jake: 'dog', finn: 'human' }
  //"jake=dog&finn=human"
  var key,
    objIn = Object.create(null),
    returnresult = '';
  objIn = this.result; //this is causing issue
  console.log('obj', objIn);
  console.log(typeof(objIn))
  for (key in objIn) {
    console.log(objIn.hasOwnProperty('key')) //trying to see wht this results in ::GIVES 'undefined is not a function error'
      //     if(objIn.hasOwnProperty(key)){
      //       returnresult += key+'='+objIn[key]+'&'
      //     }
  }
  this.result = returnresult;
  returnresult = Object.create(null);
  return this.result;
}


var test = new createObject('jake=dog&finn=human');

console.log(test);
console.log(test.toObject())
console.log(test);
console.log(test.toString());
console.log(test);

结果和错误如下:

{ result: 'jake=dog&finn=human' } 
{ jake: 'dog', finn: 'human' } 
{ result: { jake: 'dog', finn: 'human' } } 
obj { jake: 'dog', finn: 'human' } 
object 
solution.js:52 
    console.log(objIn.hasOwnProperty('key')  ) 
                      ^ 
TypeError: undefined is not a function 
    at createObject.toString (solution.js:52:23) 
    at solution.js:68:18 

这不是拼写错误,所以不确定发生了什么..

谢谢..

最佳答案

对象是引用类型。

Object.create(null) 返回真正没有原型(prototype)的空对象。例如:

var emptyObj = Object.create(null)
emptyObj.hasOwnProperty // Return undefined.
emptyObj.prototype // Return undefined.

所以:

createObject.prototype.toObject = function() {
  var objData = this.result.split('&');
  this.result = Object.create(null); //{}
  var res = Object.create(null);
  objData.forEach(function(value, index) {
    var test = value.split('=')
    return res[test[0]] = test[1];
  });

  // Here you say - this.result = res
  // But it's reference type, so the address of this variable will be
  // Setted to `this`
  this.result = res;

  // here you change the reference value of res.
  // so this.result will be = Object.create(null) after next line exec.
  res = Object.create(null);

  return this.result;

}

我认为这是问题所在。

关于javascript - 遍历原型(prototype)中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783087/

相关文章:

javascript - 是否可以测试 javascript 函数是否是构造函数?

javascript - JQuery/Javascript - 如何最小化重复代码?

javascript - ionic : Header overlaps content when subheader is hidden in tabs-based app in iOS

javascript - 是否可以确定是否从原型(prototype)方法中调用了实例方法?

javascript - 如何从需要的代码中增强 commonjs 模块?

node.js - Nodejs 调用函数和变量作用域问题;配置错误: Missing region in config

javascript - 获取对象的公共(public)方法/函数

javascript - 在 Angular 中显示来自 Rest Api 的数据的正确方法

javascript - 为什么下一个/脚本不适用于 google recaptcha?

javascript - Aframe 射线转换器 交叉点