如何在javascript中将变量传递给对象内的其他函数

时间:2016-04-19 07:42:43

标签: javascript function variables object scope

WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);

WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();

如果我运行该代码var myCar2 = { maxSpeed: 70, driver: "Arnold", drive: function(speed, time) { console.log("speed " + (speed * time)); }, logbook: function(a , b) { console.log("max speed " + this.maxSpeed + " " + this.drive(a , b)); } }; myCar2.logbook(3 , 6); this.drive(a , b)。如何使用undefined将变量传递给drive()函数?

1 个答案:

答案 0 :(得分:7)

您正在将变量传递给驱动器,并在其中定义它们。如果它们不是那么你不会在输出中得到速度18 ,你会得到速度NaN

undefined值是drive返回值,这是因为您没有在该函数中放置return语句。