相关疑难解决方法(0)

Javascript:自我和这

任何人都可以解释为什么我得到不同的自我和这个值?自我是对此的参考.

function Parent(){
   var self = this;
   this.func = function(){
      // self.a is undefined
      // this.a is 'Test'
      console.log(self.a, this.a);
   }
}

function Child(x){
   this.a = x;
}

Child.prototype.__proto__ = new Parent;
var ch = new Child('Test');
ch.func();
Run Code Online (Sandbox Code Playgroud)

我一直在项目上使用self,这是我第一次遇到这个问题.

javascript prototype this self

7
推荐指数
1
解决办法
5714
查看次数

使用变量self与此之间的区别

我一直在努力使用.bind()方法使用这些"this"并使用变量"self = this".在获得两个不同的结果时,我错过了一个概念.案例如下:

// Defining a callback class to use after retrieving data
var Callback = (function(){
    // UPDATED!! Local vbles
    var template_to_use, html_element, self;

    function Callback(){
        self = this,
        template_to_use = null,
        html_element = null;
    }

    var p = Callback.prototype;
    p.set_template = function(template_funct){
        self.template_to_use = template_funct;
    };

    p.set_html_element = function(html_element){
        self.html_element = html_element;
    };

    p.use_callback     = function(data){                                                              
        $(self.html_element).append(self.template_to_use(data));
    };

    return Callback;
})();
Run Code Online (Sandbox Code Playgroud)

该功能的用法如下:

// Setup callback 1 to call after getting the data
var callback_1 = new Callback();
callback_1.set_template(use_templ_1); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

7
推荐指数
2
解决办法
2万
查看次数

链式承诺和原型`this`

我很难获得承诺this在原型中使用正确的范围.

这是我的代码:

'use strict';

angular.module('testApp').factory('UrlSearchApi',
  function($resource, URL_SEARCH_API, PAGE_SIZE, $q){

  var resource = $resource(URL_SEARCH_API);

  resource.Scroll = function () {
    return this.reset();
  };

  resource.Scroll.prototype.reset = function () {
    this.visibleItems = [];
    this.allItems = [];
    this.busy = null;
    return this;
  };

  resource.Scroll.prototype.fetch = function(query){
    var params = {};
    if(query) { params.q = query; }
    return resource.query(params).$promise;
  };

  resource.Scroll.prototype.loadAllItems = function (results) {
    var d = $q.defer();

    angular.forEach(results, function (result, i) {
      this.allItems.push(result);
      if(i === results.length - 1 ) { d.resolve(); } …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine angularjs karma-jasmine angular-promise

5
推荐指数
1
解决办法
126
查看次数

Javascript - 范围在回调中丢失

我有以下代码:

var myPage = {};
myPage.component = function(callback){

    var somethingHappened = true;

    if (somethingHappened){
        callback();
    }
};

myPage.main = function(){

    // Initialise.
    this.init = function(){

        // make an instance of my component
        this.component = new myPage.component( this.callback );

        // need my utility function here
        this.doSomethingUseful();
    };

    // Callback to be executed when something happs in the component.
    this.callback = function(){
        this.doSomethingUseful(); // doesn't work
    };

    // A useful utility that needs to be accessible from both the 
    // init() and …
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
2469
查看次数

来自Ajax回调的Javascript(原型)方法调用

我已经编写了一些面向对象的Javascript,如下所示:

function MyClass(){

    this.SomeFunc(arg1){
        result = <some processing on arg1>;
        return result;
    };

    this.SomeOtherFunc(){
        return $.ajax({
            <some restful call>
        }).done(function(){
            var localvar = this.SomeFunc(<value obtained by restful call>);
            <some operations with localvar>;
        });
    };
};

var myObj = new MyClass();
myObj.SomeOtherFunc();
Run Code Online (Sandbox Code Playgroud)

而且我在Web控制台中收到一个错误:“ this.SomeFunc不是函数”。如果直接在函数中调用它,就没有问题。调用仅在Ajax内部失败。进行此函数调用的正确方法是什么?

javascript ajax jquery

3
推荐指数
1
解决办法
1557
查看次数