[英]Why does function getVehicleName prints the global variable vehicleName instead of variable vehicleName inside the Vehicle object?
I declared a vehicleName variable in the global scope as well as inside the Vehicle object and the fucntion printVehicleName in the global scope and getVehicleName inside the Vehicle object. 我在全局范围内以及在Vehicle对象内部声明了vehicleName变量,在全局范围内声明了fucntion printVehicleName,在Vehicle对象中声明了getVehicleName。 When i call the two functions both prints the vehicleName declared on the global scope. 当我调用这两个函数时,都会打印在全局范围内声明的vehicleName。 Why does it happen? 为什么会这样?
//declaring a global variable
let vehicleName = "Dodge";
//declaring a method to print vehicleName
function printVehicleName(){
console.log(vehicleName);
}
//declaring a object using object literal notation
let Vehicle = {
vehicleName : "Ferrari",
getVehicleName : printVehicleName
};
//executing printVehicleName function
printVehicleName();
//executing getVehicleName function
Vehicle.getVehicleName();
I expected the getVehicleName function to print "Ferrari" but When i call the two functions both prints the vehicleName declared on the global scope "dodge". 我期望getVehicleName函数打印“法拉利”但是当我调用这两个函数时,两者都打印在全局范围“dodge”上声明的vehicleName。 Can anyone explain the reasons for this and a way to print "Ferrai"? 任何人都可以解释这个的原因和打印“法拉”的方法吗?
You need to actually create a method
and use this
to set the context to the vehicleName
inside the Vehicle
object. 您需要实际创建一个method
并使用this
来将上下文设置为Vehicle
对象内的vehicleName
。
An object can refer to its self property before it is created 对象可以在创建之前引用其自身属性
let vehicleName = "Dodge"; //declaring a method to print vehicleName function printVehicleName() { console.log(vehicleName); } //declaring a object using object literal notation let Vehicle = { vehicleName: "Ferrari", getVehicleName: function() { return this.vehicleName } }; //executing printVehicleName function printVehicleName(); //executing getVehicleName function console.log(Vehicle.getVehicleName());
You can resuse printVehicleName
. 您可以重新使用printVehicleName
。 This line printVehicleName(vcName = vehicleName)
will take default vehicleName
but when calling from the getVehicleName
you can can pass the context 此行printVehicleName(vcName = vehicleName)
将采用默认的vehicleName
但是当从getVehicleName
调用时,您可以传递上下文
//declaring a global variable let vehicleName = "Dodge"; //declaring a method to print vehicleName function printVehicleName(vcName = vehicleName) { return vcName; } //declaring a object using object literal notation let Vehicle = { vehicleName: "Ferrari", getVehicleName: function() { return printVehicleName(this.vehicleName) } }; //executing printVehicleName function printVehicleName(); //executing getVehicleName function console.log(Vehicle.getVehicleName());
In your function use the keyword this. 在您的函数中使用关键字this。
this.vehicleName
Use this to access the object properties. 使用它来访问对象属性。
Below are the reason why you're getting Dodge . 以下是你获得道奇的原因。
printVehicleName()
will always refer to global variable (even if it gets assigned to a method within an object) printVehicleName()
将始终引用全局变量(即使它被分配给对象中的方法) Vehicle.getVehicleName()
is the same as printVehicleName()
Vehicle.getVehicleName()
与printVehicleName()
相同
//declaring a global variable let vehicleName = "Dodge"; //declaring a method to print vehicleName function printVehicleName(){ console.log(vehicleName); // vehicleName is global variable } //declaring a object using object literal notation let Vehicle = { vehicleName : "Ferrari", getVehicleName : printVehicleName // Referring to the global function }; //executing printVehicleName function printVehicleName(); //executing getVehicleName function Vehicle.getVehicleName(); // This is == to printVehicleName()
To have a Vehicle object that prints its own variable, you need to use this
variable. 要让Vehicle对象打印自己的变量,您需要使用this
变量。
In short, you cannot reuse printVehicleName
for a method within an object since it will no longer work properly if you print this.vehicleName
. 简而言之,您不能将printVehicleName
用于对象中的方法,因为如果您打印this.vehicleName
它将无法再正常工作。
You need a different method within an object like so. 您需要在对象中使用不同的方法。
let Vehicle = {
vehicleName: "Ferrari",
getVehicleName: function() { console.log(this.vehicleName); }
};
i changed vehicle declaration part first change the value of vehicleName then call printVehicleName function 我更改了车辆声明部分首先更改vehicleName的值然后调用printVehicleName函数
let vehicleName = "Dodge"; //declaring a method to print vehicleName function printVehicleName( ) { console.log(vehicleName); } //declaring a object using object literal notation let Vehicle = { vehicleName:"Ferrari", getVehicleName: function () { vehicleName = this.vehicleName; printVehicleName() } }; //executing printVehicleName function printVehicleName(vehicleName); //executing getVehicleName function Vehicle.getVehicleName();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.