Javascript 将参数传递给嵌套函数
这是我的代码:Javascript 将参数传递给嵌套函数,javascript,Javascript,这是我的代码: var Placemat = function(id,id2) { this.hand = document.getElementById(id); this.bucket = ""; this.bucketspace = document.getElementById(id2); this.bucketvalue = 0; var that = this; this.deal = function(id) { sh
var Placemat = function(id,id2) {
this.hand = document.getElementById(id);
this.bucket = "";
this.bucketspace = document.getElementById(id2);
this.bucketvalue = 0;
var that = this;
this.deal = function(id) {
shuffle();
var card1 = deck.shift();
var card2 = deck.shift();
this.hand.innerHTML = card1 + ", " + card2;
};
this.hit = function(id2) {
var card3 = deck.shift();
this.bucket = this.bucket + deck.shift();
this.bucketspace.innerHTML = this.bucket;
};
};
这是向嵌套函数传递参数的正确方法吗?
This.deal()
和This.hit()
中的id
和id2
来自Placemat()
否,如果要使用发送到Placemat()
的值,需要在deal()
和hit()
函数中引用它们。不要将它们列为这些函数的参数
// removed---------v
this.deal = function() {
alert( id ); // <---- do something with the original argument
shuffle();
var card1 = deck.shift();
var card2 = deck.shift();
this.hand.innerHTML = card1 + ", " + card2;
};
// removed--------v
this.hit = function() {
alert( id2 ); // <---- do something with the original argument
var card3 = deck.shift();
this.bucket = this.bucket + deck.shift();
this.bucketspace.innerHTML = this.bucket;
};
//已删除--------v
this.deal=函数(){
警报(id);//否。事实上,您首先传递的id是什么?在函数中不使用它们
// removed---------v
this.deal = function() {
alert( id ); // <---- do something with the original argument
shuffle();
var card1 = deck.shift();
var card2 = deck.shift();
this.hand.innerHTML = card1 + ", " + card2;
};
// removed--------v
this.hit = function() {
alert( id2 ); // <---- do something with the original argument
var card3 = deck.shift();
this.bucket = this.bucket + deck.shift();
this.bucketspace.innerHTML = this.bucket;
};
按照这种方式,您正在创建两个函数(hit
和deal
),每个函数都需要一个参数。这些参数恰好与外部函数的参数命名相同。我看不出这些参数的作用是什么?