财产更新失败 - JavaScript'这个'没有指向正确的对象

时间:2017-03-09 10:51:21

标签: javascript object inheritance prototype

我有一个Person构造函数,可以获取或设置一个人的第一个,最后一个和全名。

var claude = new Person('Claude Shannon');
claude.getFullName();

让方法按预期工作......

claude.setFirstName('james');
claude.getFullName();  //  "Claude Shannon"  

但为什么不进行以下工作呢?

Require Import List.
Import ListNotations.

Fixpoint count {T : Type} (p : T -> bool) (l : list T) :=
  match l with
  | [] => 0
  | h :: t => if p h then S (count p t) else (count p t)
  end.

Inductive T := a | b | c.

Definition q x :=
  match x with
  | a => true
  | b => true
  | c => false
  end.

Lemma example: (count q [a; b; c]) = 2.
Proof.
  reflexivity.
Qed.

(显然我期待" James Shannon

4 个答案:

答案 0 :(得分:1)

很简单,您实际上永远不会返回新的全名(由setFirstNamesetLastName设置的两个属性组成)。你从构造函数中返回一个。

this.getFullName = function(){
  var full = this.fullName || (function(){
    var first = firstAndLast.split(' ')[0];
    var last = firstAndLast.split(' ')[1];
    return first + " " + last;
  })();
  return full;
};

VS

this.getFullName = function(){
  return this.getFirstName() + " " + this.getLastName()
};

var Person = function(firstAndLast) {
    var self = this;
    this.getFirstName = function(){
      var first = this.firstName || (function(){
        return firstAndLast.split(' ')[0];

      })();
      return first;
    };
    this.getLastName = function(){
      var last = this.lastName || (function(){
        return firstAndLast.split(' ')[1];

      })();
      return last;
    };
    this.getFullName = function(){
      return this.getFirstName() + " " + this.getLastName()
    };
    this.setFirstName = function(first){
      self.firstName = first;
      console.log('first name is now: ' + self.firstName);
    };
    this.setLastName = function(last){
      self.lastName = last;
      console.log('last name is now: ' + self.lastName);
    };
    this.setFullName = function(firstAndLast){
      self.fullName = firstAndLast;
      console.log('full name is now: ' + self.fullName);
    };
};

var claude = new Person('Claude Shannon');
console.log(claude.getFullName());

claude.setFirstName('James');
console.log(claude.getFullName()); //  "Claude Shannon"

答案 1 :(得分:0)

我建议将所有方法都放入原型中,并在任何地方使用它。

但问题是getFullName()会查看this.fullName属性。 由于您使用了setFirstName(),它只将this.firstName设置为James,因此getFullName会继续回顾构造函数中使用的参数,即' Claude Shannon',因为this.fullName未定义。

答案 2 :(得分:0)

您正在从传递给构造函数的初始参数中读取全名 - 它永远不会更新。您需要从属性中读取。获取名字或姓氏时,你会从动态属性中读取,但是对于全名,你总是从那个初始构造函数arg中读取。

此外,没有理由写信给self - this没问题。

我做了一些其他的改变。 Fiddle

var Person = function(first, last) {
    //var self = this; <-- unnecessary
    this.getFirstName = function(){
      var first = this.firstName || (function(){
        return firstAndLast.split(' ')[0];

      })();
      return first;
    };
    this.getLastName = function(){
      var last = this.lastName || (function(){
        return firstAndLast.split(' ')[1];

      })();
      return last;
    };
    this.getFullName = function(){
      //much simpler than before - just concatenate properties
      return this.firstName+' '+this.lastName;
    };
    this.setFirstName = function(first){
      this.firstName = first;
      alert('first name is now: ' + this.firstName);
    };
    this.setLastName = function(last){
      this.lastName = last;
      console.log('last name is now: ' + this.lastName);
    };
    this.setFullName = function(firstAndLast){
      this.fullName = firstAndLast;
      console.log('full name is now: ' + this.fullName);
    };
    this.setFirstName(first); //set initial first name passed to construc
    this.setLastName(last);   //" " last " " " "
};

var claude = new Person('Claude','Shannon'); //注意,两个单独的args claude.setFirstName( '詹姆斯'); 警报(claude.getFullName()); //“James Shannon”

答案 3 :(得分:0)

在你的getFullName函数中,你没有检查第一个名字。您只是检查全名是否存在。即使您在代码中更正了上下文,仍然会遇到问题,因为逻辑。

<activity/>