JavaScript - 无法访问函数

时间:2017-01-31 23:47:56

标签: javascript function

需要帮助从以下代码访问saluteFriends和sayHelloLater方法。我对方法范围感到困惑。猜测是因为这两种方法都是私有的,因此无法访问。

$(function() {
  $('a[href^="http"]').each(function() {
    var $a = $(this),
        q = $a.attr('href') ? $a.attr('href').indexOf('?') : -1,
        contents = $a.html(),
   // the next line overwrites the global value 
   // of placement if ID is defLink
        placement = ($a.attr('id') === "defLink") ? "defLink":placement;
    if (q == -1) {
      $a.attr('href', $a.attr('href') + '?group=' + '&placement=' + placement);
    } else {
      $a.attr('href', $a.attr('href') + '&group=' + '&placement=' + placement);
    }
  });
});

2 个答案:

答案 0 :(得分:0)

function Person(name, friends) {
    // friends is a list of strings 
    this.say = function (sentence) {
        console.log(name + ' says: ' + sentence);
    };

    Person.prototype.sayHello = function (otherName) {
        this.say('hello ' + otherName + '!');

    };

    this.saluteFriends = function () {
        friends.forEach(function (friend) {
            this.sayHello(friend);
        }.bind(this));
    };

    this.sayHelloLater = function (delay, otherName) {
        setTimeout(function () {
            this.sayHello(otherName);
        }, delay);
    };
}

var frnds = ["sam", "mathew"];
var fcall = new Person("alan", frnds);
fcall.saluteFriends();

您需要将函数say作为方法附加到正在创建的对象上。因此,将var say更改为this.say。然后,您需要.bind this上下文forEach方法saluteFriends内的this方法。否则forEach内的sayHello指的是回调函数。然后,您需要将this置于其前面来调用this.sayHello,如下所示:for i =1:nbr arduino_serial= serial('/dev/cu.wchusbserial1410'); set(arduino_serial,'BaudRate',9600); set(arduino_serial,'Terminator','CR'); fopen(arduino_serial); pause(1); A_string = strcat(num2str(tabx(i)),',',num2str(taby(i)),',',num2str(pression_ref)); %Send fprintf(arduino_serial,A_string); %Receive from Arduino while (strcmp('Ready',fscanf(arduino_serial,'%f'))==0) end fclose(arduino_serial); delete (arduino_serial); end

答案 1 :(得分:0)

原型上的函数有一些差异,创建一次并在每个实例之间共享,而不是在构造函数中创建的函数,为新对象创建 < strong>每个使用构造函数创建的新对象。

因此,您无法使用this关键字调用原型函数。此外,您已将say()函数声明为对象的私有成员,因此您不需要this关键字。

function Person(name, friends) {
    // friends is a list of strings 
    var say = function (sentence) {
        console.log(name + ' says: ' + sentence);
    };

    Person.prototype.sayHello = function (otherName) {
        //say function is a private function, no need of "this" keyword
        say('hello ' + otherName + '!');

    };

    this.saluteFriends = function () {
        friends.forEach(function (friend) {
            //sayHello function is attached to the prototype, bind the context
            this.sayHello(friend);
        }.bind(this));
    };

    this.sayHelloLater = function (delay, otherName) {
        setTimeout(function () {
            this.sayHello(otherName);
        }.bind(this), delay);
    };
}

var frnds = ["sam", "mathew"];
var fcall = new Person("alan", frnds);
fcall.saluteFriends();
fcall.sayHelloLater(1000, "john");

为了您的信息,我在ES2015中制作了相同的代码,这样我们就可以使用classarrow functionstring interpolation。此外,箭头函数不绑定自己的this

class Person {

  constructor(name, friends) {
    this.name = name;
    this.friends = friends;
  }

  say(sentence) {
    console.log(`${this.name} says: ${sentence}`);
  }

  sayHello(otherName) {
    this.say(`hello ${otherName}`);
  }

  saluteFriends() {
    this.friends.forEach(friend => this.sayHello(friend));
  }

  sayHelloLater(delay, otherName) {
    setTimeout(() => this.sayHello(otherName), delay);
  }

}

const person = new Person("paul", frnds);
person.saluteFriends();
person.sayHelloLater(1000, "jack");