为什么在控制台中使用以下方法却没有在最后使用console.log?

时间:2018-12-03 13:33:42

标签: javascript

嗨,我想知道为什么我在不使用console.log(jakubpresentation)的情况下设置以下变量时仍能看到它的原因?

var jakubpresentations = jakub.presentation("formal", " morning ");

var jakub = {
  name: "jakub",
  surname: "klos",
  age: 18,
  profession: "designer",
  presentation: function(style, timeOfDay) {

    if (style === "formal") {
      console.log("good " + timeOfDay + this.name + " Welcome in DesignUX Company")
    } else if (style === "nonformal") {
      console.log("Hi" + this.name + "Welcome in DesignUX Company");
    }
  }
}

var justyna = {
  name: "justyna",
  surname: "rybicka",
  age: 28
}

var jakubpresentations = jakub.presentation("formal", " morning ");

1 个答案:

答案 0 :(得分:1)

您将jakubpresentations的值设置为从函数jakub.presentation返回的值。

var jakubpresentations = jakub.presentation("formal", " morning ");

当您将变量设置为等于函数调用时,该函数将运行。整个功能将执行。 console.log语句在您运行的函数中。

presentation: function(style, timeOfDay) {
  if (style === "formal") {
    console.log("good " + timeOfDay + this.name + " Welcome in DesignUX Company")
  } else if (style === "nonformal") {
    console.log("Hi" + this.name + "Welcome in DesignUX Company");
  }
}

您可以通过使用函数来创建变量来运行它。您不必自己给它一整行。