在没有“this”的情况下访问JavaScript中对象中的变量

发布于 2024-10-04 00:26:00 字数 601 浏览 3 评论 0原文

function Peon(number) {
  this.number = number;

  this.inc = function() {
    number = number + 1;
  };

  return true;
}
var p = new Peon(10);

function returnNumber() {
  p.inc();
  alert(p.number);
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

该代码无法按预期工作。有没有一种方法可以让它工作而不必写

this.number=this.number+1;

Here 这是一个微不足道的选择,但在更大的代码中没有 this.* 会让它更具可读性。是否可以?

function Peon(number) {
  this.number = number;

  this.inc = function() {
    number = number + 1;
  };

  return true;
}
var p = new Peon(10);

function returnNumber() {
  p.inc();
  alert(p.number);
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

This code doesn't work as intended. Is there a way to make it work without having to write

this.number=this.number+1;

Here it is a trivial choice, but in bigger codes not having this.* would make it a lot more readable. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对岸观火 2024-10-11 00:26:01

我能看到的唯一可读的方式是:

this.inc = function() {
   this.number++;
};

否则,在你的“更大的代码”假设中,你可以这样做:

this.inc = function() {
   var number = this.number; // obviously simple here.  Imagine more complexity
   number++;
};

The only readable way I can see doing it would be:

this.inc = function() {
   this.number++;
};

Otherwise, in your "bigger codes" postulation, you could do something like this:

this.inc = function() {
   var number = this.number; // obviously simple here.  Imagine more complexity
   number++;
};
亚希 2024-10-11 00:26:01

是的,您不需要在 JavaScript 中使用“this”。您可以通过闭包而不是“this”访问变量

function createPeon(number) {

  function inc() {
    number = number + 1;
  };

  function getNumber() {
    return number;
  }

  return {
    inc,
    getNumber
  };
}
var p = createPeon(10);

function returnNumber() {
  p.inc();
  alert(p.getNumber());
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

Yes, you do not need to use 'this' in javascript. You can access variables via closure instead of 'this'

function createPeon(number) {

  function inc() {
    number = number + 1;
  };

  function getNumber() {
    return number;
  }

  return {
    inc,
    getNumber
  };
}
var p = createPeon(10);

function returnNumber() {
  p.inc();
  alert(p.getNumber());
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

守护在此方 2024-10-11 00:26:00

您可以将 number 设置为“私有”,但是您需要一个 getter

function Peon(number) {
  var number = number;

  // increment
  this.inc = function() {
    number++;
  };

  // a simple getter
  this.getNumber = function() {
    return number;
  }
}

var p = new Peon(10);

function returnNumber() {
  p.inc();
  alert(p.getNumber());
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

您应该阅读 Douglas Crockfords 的“The Good Parts”,了解有关如何使用此模式的更多信息,在 Google 图书

另外,您不需要从构造函数返回某些内容,您的 return true 是多余的。

You can make number "private", but then you need a getter:

function Peon(number) {
  var number = number;

  // increment
  this.inc = function() {
    number++;
  };

  // a simple getter
  this.getNumber = function() {
    return number;
  }
}

var p = new Peon(10);

function returnNumber() {
  p.inc();
  alert(p.getNumber());
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

You should read Douglas Crockfords "The Good Parts" for more information on how to use this pattern, there's (limited) a preview available at Google Books.

Also you don't need to return something from the constructor, your return true is superfluous.

臻嫒无言 2024-10-11 00:26:00

不可以,您必须使用 this 来引用 this 对象上的属性。请注意,JavaScript 中的 this 与其他一些语言(例如 C 或 Java)中的 this 非常不同。更多此处此处

您的代码所做的是访问传递给 Peon 构造函数的 number 参数,而不是您在中创建的 this.number 属性构造函数。这就是为什么它不能按预期工作,但也不会失败。

没有理由在 Peon 构造函数中定义 inc 操作,顺便说一句,并且有一些很好的理由不这样做(通过 < code>Peon 将获得该函数的自己的副本)。因此,您可以这样定义它:

function Peon(number) {
    this.number = number;
  
    // Don't return values out of constructor functions, it's
    // an advanced thing to do. In your case, you were returning
    // `true` which was being completely ignored by the JavaScript
    // engine because it's not an object. If you had returned an
    // object, the `this` object created for the `new Peon()` call
    // would have been thrown away and the object you returned
    // used instead.
}

Peon.prototype.inc = function() {
    ++this.number;
};

var p = new Peon(10);

function returnNumber() {
    p.inc();
    console.log(p.number); // shows 11, then 12, etc.
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

或者使用现代 JavaScript 功能 (ES2015+):

class Peon {
    constructor(number) {
        this.number = number;
    }
    
    inc() {
        ++this.number;
    }
}

const p = new Peon(10);

function returnNumber() {
    p.inc();
    console.log(p.number); // shows 11, then 12, etc.
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

No, you have to use this to reference properties on the this object. Note that this in JavaScript is very different from this in some other languages, like C or Java. More here and here.

What your code is doing is accessing the number argument that was passed into the Peon constructor function, rather than the this.number property you created in the constructor. Which is why it doesn't work as intended, but doesn't fail, either.

There's no reason to define your inc operation within the Peon constructor function, BTW, and some good reasons not to (every individual object created via Peon will get its very own copy of that function). So instead, you can define it like this:

function Peon(number) {
    this.number = number;
  
    // Don't return values out of constructor functions, it's
    // an advanced thing to do. In your case, you were returning
    // `true` which was being completely ignored by the JavaScript
    // engine because it's not an object. If you had returned an
    // object, the `this` object created for the `new Peon()` call
    // would have been thrown away and the object you returned
    // used instead.
}

Peon.prototype.inc = function() {
    ++this.number;
};

var p = new Peon(10);

function returnNumber() {
    p.inc();
    console.log(p.number); // shows 11, then 12, etc.
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

Or using modern JavaScript features (ES2015+):

class Peon {
    constructor(number) {
        this.number = number;
    }
    
    inc() {
        ++this.number;
    }
}

const p = new Peon(10);

function returnNumber() {
    p.inc();
    console.log(p.number); // shows 11, then 12, etc.
}
<input id="b00" type="button" value="Click" onclick="returnNumber()">

鹤仙姿 2024-10-11 00:26:00

不是真的,但这更简洁

this.number++

实际上,作为旁注,您最好在 Peon 的构造函数之外声明 .inc。你可以用原型来做到这一点。这样,每次创建 Peon 类型的对象时,就不会重建 inc 函数。

Peon.prototype.inc = function(){
    this.number++;
}

或者,您可以使用 p.number++,而不是使用 p.inc()。这是我能想到避免使用 this 关键字的唯一方法。

Not really, but this is a little more concise

this.number++

Actually, as a side note, you'd be better off declaring .inc outside the constructor of Peon. You could do this with prototype. That way the inc function is not reconstructed each time you create an object of type Peon.

Peon.prototype.inc = function(){
    this.number++;
}

Or instead of using p.inc() you could do p.number++. That's the only way I can think of avoiding the this keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文