方法,循环和“ this”关键字

时间:2019-05-30 16:48:01

标签: javascript object this

我是Java语言的新手。我目前正在查看关键字this和方法以及如何返回字符串。 我正在努力使用关键字this返回字符串。

我已经成功创建了返回字符串的代码,但是问题是它显示了“对象已定义”的错误。

这是我正在进行的练习,也是我尝试创建的代码,它们未能返回正确的结果:

function exerciseTwo(userObj) {

  // Exercise Two: You will be given an object called 'userObj'
  // userObject will already have a key on it called 'name'
  // Add a method to userObj, called 'greeting'.
  // Using the keyword 'this', the greeting method should return the following string:
  // 'Hi, my name is ' and the users name.
  // eg: If userObj has a name: 'Dan', greeting should return: Hi, my name is Dan'
  // NOTE: DO NOT create a new object.
  // NOTE: DO NOT create a key called name the key is already on the object.

  let userObj = {
    name: "Lisa",
    greeting: function() {
      return "Hi, my name is " + this.name;
    },
  };
  console.log(userObj.greeting());
}

//In the first line of code it shows a error which says that "userObj" is already defined. So I do not know how to return the string without creating a new object and creating a key called name.

//Here is another option I tried but it also did not work out:

function greeting() {
  this.name = "Lisa";
  let result = "Hi, my name is " + this.name;
  return result;
},
userObj.greeting();
}

//The exercise should add a greeting method to userObject object.

因此,如果userObj的名称为:“ Lisa”,则问候语应返回:“嗨,我叫Lisa”

3 个答案:

答案 0 :(得分:1)

问题是您的局部变量与函数参数具有相同的名称。您应该在现有变量中添加一个方法,而不是创建一个新变量。说明特别指出“请勿创建新对象”,但这就是您所做的。

function exerciseTwo(userObj) {

  // Exercise Two: You will be given an object called 'userObj'
  // userObject will already have a key on it called 'name'
  // Add a method to userObj, called 'greeting'.
  // Using the keyword 'this', the greeting method should return the following string:
  // 'Hi, my name is ' and the users name.
  // eg: If userObj has a name: 'Dan', greeting should return: Hi, my name is Dan'
  // NOTE: DO NOT create a new object.
  // NOTE: DO NOT create a key called name the key is already on the object.
  
  userObj.greeting = function() {
    return "Hi, my name is " + this.name;
  };
  console.log(userObj.greeting());
}

let obj = {
  name: "Joe"
};
exerciseTwo(obj);

答案 1 :(得分:1)

function exerciseTwo(userObj){ // The argument for this "exerciseTwo" function has been declared as "userObj"

  let userObj = { // Here you are trying to declare another variable "userObj"
    name: "Lisa",
    greeting: function() {
      return "Hi, my name is " + this.name;
    }
  };

  console.log(userObj.greeting());
}

要解决您的问题, -在“ exerciseTwo”函数外部声明let userObj = { ... }块,并将其作为变量传递

let lisa = {
  name: "Lisa"
};

function exerciseTwo(userObj){ // Whatever variable you pass into this function will be synonymous to `userObj` within this function
  userObj.greeting = function () {
    return "Hi, my name is " + this.name;
  }
  console.log(userObj.greeting());
}

exerciseTwo(lisa) // lisa will take the position of `userObj` above

答案 2 :(得分:0)

如练习所述,您只需要向用户对象添加问候功能。像这样:

let userObj = { name: "Lisa" };

function exercise2(userObj) {
    userObj.greetings = function () {
        return "Hi, my name is " + this.name;
    }
}

exercise2(userObj);

console.log(userObj.greetings());