在javascript中打印类中的变量
伙计们,我有简单的代码。。。但它打印出来还不确定,但我不知道为什么在javascript中打印类中的变量,javascript,Javascript,伙计们,我有简单的代码。。。但它打印出来还不确定,但我不知道为什么 函数myMove(){ 此值为0.pos; this.right=函数(){ 乐趣(); } 函数fun(){ 这个.pos++; } } var move=new myMove(); 警惕(move.right())您需要从fun()和this.right返回值 function myMove() { var pos = 0; this.right = function() { return fun()
函数myMove(){
此值为0.pos;
this.right=函数(){
乐趣();
}
函数fun(){
这个.pos++;
}
}
var move=new myMove();
警惕(move.right())
您需要从fun()
和this.right
返回值
function myMove() {
var pos = 0;
this.right = function() {
return fun();
}
function fun(){
return pos++;
}
}
var move = new myMove();
alert(move.right());
试试这个
初始化时需要使用pos
而不是this.pos
函数myMove(){
pos=0;
this.right=函数(){
返回乐趣();
}
函数fun(){
这个.pos++;
返回此.pos;
}
}
var move=new myMove();
警惕(move.right())代码>
函数myMove(){
pos=0;
this.right=函数(){
返回乐趣();
}
函数fun(){
返回++this.pos;
}
}
var move=new myMove();
警惕(move.right())
右侧
不返回任何内容。您希望它打印什么?@Carcigenicate希望它打印“1”this.pos++
->返回此。pos++
fun()代码>->返回乐趣()
你能解释为什么吗?顺便说一句,它不是“1”Oh-pos没有定义,因为pos和fun()上下文的这个不一样。检查我编辑的答案。