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()">