未定义的回调在对象内创建对象

时间:2017-01-21 21:45:52

标签: javascript object this

我是Javascript的新手。我试图创建一个使用回调函数来显示字符串的滑块;准确地说,options.data是一个字符串数组,我需要使用滑块的输入从中打印正确的字符串。当我运行脚本时,_getString方法是未定义的,所以它没有工作。我怎样才能做到这一点?

function Obj( options ) {

this.opz1 = options.opz1;
this.opz2 = options.opz2;
this.data = options.data;   // Data is an Array..

this._getString = function( val ) {

  return 'Value: ' +this.data[val].label;
};

this.update = function () {

  console.log("Using slider!");
};  

this.init = function () {

  var sliderCfg = {

    position: 'position!',
    printValue: this._getString,
    width: '300px',
    value: 0
  };

  var slider = new Slider( update, this.sliderCfg );

  };

  this.init();
}

var a = new Obj( opz );

编辑:jQuery.Deferred异常:无法读取属性'标签'未定义的TypeError:无法读取属性'标签'未定义的

Edit2:具有相同问题的简化方案> https://jsfiddle.net/nhzm8ctt/



var array = [ 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10'];

config = {

  "data": array,
  "funzione": 'boh'
};
 
function Obj ( options ) {

  this.data = options.data;

  this._getString = function ( val ) {
 
    return 'Output is: ' +this.data[0];
  };

  this.init = function () {

    var anotherObj = new AnotherObj( this._getString );
  };

  this.init();
}

function AnotherObj( f ) {

  this.init = function () {

    var value = 0;

    console.log( "f: ", f(value) );
  };

  this.init();
}

var prova = new Obj( config );




0 个答案:

没有答案