2012-05-13 3 views
0

저는 프로토 타입을 처음 사용하기 시작했으며 개발중인 웹 사이트에서 여러 가지 것을 테스트하려고합니다. 그러나 나는 실수를해서 우연히 만났고 그 이유를 모르겠습니다. 전에 그 속성을 사용했기 때문에 작동했습니다.유형 프로토 타입을 사용할 때의 오류

나는 아직도 배우고, 더 숙련 된 자바 스크립트 개발자가 아닌 없지만, 여기에 내가 가진 무엇 :

var defaults = { 
    local_storage_key : "Cluster", 
    plugin_navigation : ".navigation", 
    plugin_wrapper  : "content-wrapper", 
    iqns_class   : ".iqn" 
} 

var Clusters = function(environment, options){ 

    this.options = $.extend({}, defaults, options); 

    this.environment = environment; 

    this.viewport = $(window); 

    this.viewport_width = this.viewport.width(); 
    this.viewport_height = this.viewport.height(); 

    this.data_key = this.options.local_storage_key; 

    this.iqns_class = this.viewport.find(this.options.iqns_class); 

    this.iqn = this.iqns_class.parent(); 

    this.shop_initiated = false; 

    this.plugin_navigation = this.options.plugin_navigation; 

    this.plugin_wrapper = this.options.plugin_wrapper; 

    this.initiate_plugin(this.plugin_navigation, { 
     containerID : this.plugin_wrapper, 
     first  : false, 
     previous : false, 
     next  : false, 
     last  : false, 
     startPage : this.get_local_storage_data(), 
     perPage  : 6, 
     midRange : 15, 
     startRange : 1, 
     endRange : 1, 
     keyBrowse : false, 
     scrollBrowse: false, 
     pause  : 0, 
     clickStop : true, 
     delay  : 50, 
     direction : "auto", 
     animation : "fadeInUp", 
     links  : "title", 
     fallback : 1000, 
     minHeight : true, 
     callback : function(pages) { 
      this.set_local_storage_data(pages.current); 
     } 
    }); 

    this.initiate_auxiliars(); 

    this.set_environment(); 

}; 

Clusters.prototype.set_local_storage_data = function(data_val) { 
    return localStorage.setItem(this.data_key, data_val); 
}; 

Clusters.prototype.get_local_storage_data = function() { 
    return +(localStorage.getItem(this.data_key) || 1); 
}; 

Clusters.prototype.shop_iqns_selected_class = function() { 
    var self = this; 
    if (this.viewport_width < 980) { 
     $(this.iqns_class).each(function(index, element) { 
      var element = $(element); 
      $(self.iqn).on('click', function() { 
       if (element.hasClass('selected')) { 
        element.removeClass('selected'); 
       } else { 
        element.addClass('selected'); 
       } 
      }); 
     }); 
    } 
} 

Clusters.prototype.initiate_plugin = function(plugin_navigation, plugin_options) { 
    return $(plugin_navigation).jPages(plugin_options); 
} 

Clusters.prototype.initiate_auxiliars = function() { 
    return this.shop_iqns_selected_class(); 
} 

Clusters.prototype.set_environment = function() { 
    if(this.environment == "Development") { 
     less.env = "development"; 
     less.watch(); 
    } 
} 

var cluster = new Clusters("Development"); 

나는 때문에, 그렇지 않으면, 내가 잘못 또는 프로토 타이핑에 대한 오해하고있어 뭔가가있을거야 나는 어떤 오류도 내지 않을 것이다. 나는 내가하고있는 일에 대한 몇 가지 의견이나 몇 가지 지시를 요구하고 있으며, 내가해야 할 일과해야 할 일은하지 말아야한다.

Uncaught TypeError: Object #<Object> has no method 'set_local_storage_data' on line 53 

답변

1

당신이 콜백 기능를 사용하는 것처럼이의 this keyword 클러스터는 더 이상 객체를 참조하지 않습니다

내가 얻을 오류입니다. 콜백은 "set_local_storage_data '메소드가없는 다른 컨텍스트에서 실행됩니다.

것을 방지하려면, 당신이 중 하나를 수행 할 수 있습니다 bind() 클러스터 객체에 기능, 클러스터 개체를 참조하는 생성자의 범위에 지역 변수를 사용하거나 :

var that = this; 
this.initiate_plugin(
    ..., 
    function(pages) { 
     that.set_local_storage_data(pages.current); 
    } 
); 
관련 문제