Grouping methods without losing the use of the this keyword [duplicate]
This question already has an answer here:
Organize prototype javascript while perserving object reference and inheritance
3 answers
In the following code how do I get the this keyword to bind to Square?
Square.prototype.is = {
king: function () { this.piece === "K" }
queen: function () { this.piece === "Q" }
...
};
I know I can later on use call/apply but the whole point was to get from
this.isKing()
to
this.is.king()
making it more readable (also grouping the methods together)
this.is.king.apply(this)
seems like a step backwards.
javascript methods this grouping
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Organize prototype javascript while perserving object reference and inheritance
3 answers
In the following code how do I get the this keyword to bind to Square?
Square.prototype.is = {
king: function () { this.piece === "K" }
queen: function () { this.piece === "Q" }
...
};
I know I can later on use call/apply but the whole point was to get from
this.isKing()
to
this.is.king()
making it more readable (also grouping the methods together)
this.is.king.apply(this)
seems like a step backwards.
javascript methods this grouping
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
What doesthisrefer to?thisrefering to a Chess instance orthisreferring to some other object?
– kemicofa
Nov 24 '18 at 13:04
I edited my post, the class in questions is namedSquarenotChess. All thethiskeywords refer to aSquareinstance
– David
Nov 24 '18 at 13:15
What if you returnthison both king and queen fns
– enapupe
Nov 24 '18 at 13:20
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what thethiskey refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion
– David
Nov 24 '18 at 13:29
add a comment |
This question already has an answer here:
Organize prototype javascript while perserving object reference and inheritance
3 answers
In the following code how do I get the this keyword to bind to Square?
Square.prototype.is = {
king: function () { this.piece === "K" }
queen: function () { this.piece === "Q" }
...
};
I know I can later on use call/apply but the whole point was to get from
this.isKing()
to
this.is.king()
making it more readable (also grouping the methods together)
this.is.king.apply(this)
seems like a step backwards.
javascript methods this grouping
This question already has an answer here:
Organize prototype javascript while perserving object reference and inheritance
3 answers
In the following code how do I get the this keyword to bind to Square?
Square.prototype.is = {
king: function () { this.piece === "K" }
queen: function () { this.piece === "Q" }
...
};
I know I can later on use call/apply but the whole point was to get from
this.isKing()
to
this.is.king()
making it more readable (also grouping the methods together)
this.is.king.apply(this)
seems like a step backwards.
This question already has an answer here:
Organize prototype javascript while perserving object reference and inheritance
3 answers
javascript methods this grouping
javascript methods this grouping
edited Nov 24 '18 at 13:13
David
asked Nov 24 '18 at 13:01
DavidDavid
255
255
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 14:32
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
What doesthisrefer to?thisrefering to a Chess instance orthisreferring to some other object?
– kemicofa
Nov 24 '18 at 13:04
I edited my post, the class in questions is namedSquarenotChess. All thethiskeywords refer to aSquareinstance
– David
Nov 24 '18 at 13:15
What if you returnthison both king and queen fns
– enapupe
Nov 24 '18 at 13:20
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what thethiskey refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion
– David
Nov 24 '18 at 13:29
add a comment |
1
What doesthisrefer to?thisrefering to a Chess instance orthisreferring to some other object?
– kemicofa
Nov 24 '18 at 13:04
I edited my post, the class in questions is namedSquarenotChess. All thethiskeywords refer to aSquareinstance
– David
Nov 24 '18 at 13:15
What if you returnthison both king and queen fns
– enapupe
Nov 24 '18 at 13:20
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what thethiskey refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion
– David
Nov 24 '18 at 13:29
1
1
What does
this refer to? this refering to a Chess instance or this referring to some other object?– kemicofa
Nov 24 '18 at 13:04
What does
this refer to? this refering to a Chess instance or this referring to some other object?– kemicofa
Nov 24 '18 at 13:04
I edited my post, the class in questions is named
Square not Chess. All the this keywords refer to a Square instance– David
Nov 24 '18 at 13:15
I edited my post, the class in questions is named
Square not Chess. All the this keywords refer to a Square instance– David
Nov 24 '18 at 13:15
What if you return
this on both king and queen fns– enapupe
Nov 24 '18 at 13:20
What if you return
this on both king and queen fns– enapupe
Nov 24 '18 at 13:20
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what the
this key refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion– David
Nov 24 '18 at 13:29
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what the
this key refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion– David
Nov 24 '18 at 13:29
add a comment |
2 Answers
2
active
oldest
votes
I would suggest using ES6 arrow functions. This is a syntax that is supported by all major browsers now. Using arrow functions allows us to leverage the outer scope's this.
An example using Class:
class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());If you're determined to use ES5 syntax:
var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());Finally, per @Bergi's comment, here's one more approach, using arrow functions and objects, without any Classes.
const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());
I have a lot of methods. Myclass Squareblock would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?
– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't needclasses at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.
– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
|
show 1 more comment
I know this does not answer directly to your question, but the is functions that are commonly found in third party libraries usually have the following signature: is(type).
In this fashion, you could write:
square.prototype.is = function(type) {
switch (String(type).toLowerCase()) {
case "king": return this.piece === "K";
case "queen": return this.piece === "Q";
default: return false;
}
}
Assuming type would be a String. But it can an Number defined in an Object to act as a Enum.
I hope you won't consider this off-topic as it can be a solution to consider alternative ways :)
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would suggest using ES6 arrow functions. This is a syntax that is supported by all major browsers now. Using arrow functions allows us to leverage the outer scope's this.
An example using Class:
class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());If you're determined to use ES5 syntax:
var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());Finally, per @Bergi's comment, here's one more approach, using arrow functions and objects, without any Classes.
const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());
I have a lot of methods. Myclass Squareblock would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?
– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't needclasses at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.
– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
|
show 1 more comment
I would suggest using ES6 arrow functions. This is a syntax that is supported by all major browsers now. Using arrow functions allows us to leverage the outer scope's this.
An example using Class:
class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());If you're determined to use ES5 syntax:
var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());Finally, per @Bergi's comment, here's one more approach, using arrow functions and objects, without any Classes.
const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());
I have a lot of methods. Myclass Squareblock would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?
– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't needclasses at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.
– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
|
show 1 more comment
I would suggest using ES6 arrow functions. This is a syntax that is supported by all major browsers now. Using arrow functions allows us to leverage the outer scope's this.
An example using Class:
class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());If you're determined to use ES5 syntax:
var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());Finally, per @Bergi's comment, here's one more approach, using arrow functions and objects, without any Classes.
const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());I would suggest using ES6 arrow functions. This is a syntax that is supported by all major browsers now. Using arrow functions allows us to leverage the outer scope's this.
An example using Class:
class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());If you're determined to use ES5 syntax:
var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());Finally, per @Bergi's comment, here's one more approach, using arrow functions and objects, without any Classes.
const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());class Square {
constructor(){
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
}
}
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());var Square = function Square() {
var _this = this;
this.piece = null;
this.is = {
king: function() { return _this.piece === "K" },
queen: function() { return _this.piece === "Q" }
};
};
var square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());const Square = function Square() {
this.piece = null;
this.is = {
king: () => this.piece === "K",
queen: () => this.piece === "Q"
};
};
const square = new Square();
square.piece = 'K';
console.log(square.is.king());
console.log(square.is.queen());edited Nov 24 '18 at 14:45
answered Nov 24 '18 at 13:22
Matt MorganMatt Morgan
2,3362820
2,3362820
I have a lot of methods. Myclass Squareblock would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?
– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't needclasses at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.
– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
|
show 1 more comment
I have a lot of methods. Myclass Squareblock would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?
– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't needclasses at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.
– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
I have a lot of methods. My
class Square block would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?– David
Nov 24 '18 at 13:37
I have a lot of methods. My
class Square block would have a few hundred lines of code. Do you still recommend doing this? Is combining the .prototype way of defining methods and the class way an option?– David
Nov 24 '18 at 13:37
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
Of the two approaches, it seems like the ES6 class-based approach is the less verbose, although they are very similar in length.
– Matt Morgan
Nov 24 '18 at 14:09
This doesn't need
classes at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.– Bergi
Nov 24 '18 at 14:31
This doesn't need
classes at all, the main feature of your approach are arrow functions that are created in the scope of the constructor.– Bergi
Nov 24 '18 at 14:31
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
@Bergi true enough. I've added one more simplified approach. Also cleaned up the ES5 example...
– Matt Morgan
Nov 24 '18 at 14:34
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
I just wanted you to edit the first sentence from "I would suggest using ES6 classes." to "I would suggest using ES6 arrow functions." :-)
– Bergi
Nov 24 '18 at 14:39
|
show 1 more comment
I know this does not answer directly to your question, but the is functions that are commonly found in third party libraries usually have the following signature: is(type).
In this fashion, you could write:
square.prototype.is = function(type) {
switch (String(type).toLowerCase()) {
case "king": return this.piece === "K";
case "queen": return this.piece === "Q";
default: return false;
}
}
Assuming type would be a String. But it can an Number defined in an Object to act as a Enum.
I hope you won't consider this off-topic as it can be a solution to consider alternative ways :)
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
add a comment |
I know this does not answer directly to your question, but the is functions that are commonly found in third party libraries usually have the following signature: is(type).
In this fashion, you could write:
square.prototype.is = function(type) {
switch (String(type).toLowerCase()) {
case "king": return this.piece === "K";
case "queen": return this.piece === "Q";
default: return false;
}
}
Assuming type would be a String. But it can an Number defined in an Object to act as a Enum.
I hope you won't consider this off-topic as it can be a solution to consider alternative ways :)
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
add a comment |
I know this does not answer directly to your question, but the is functions that are commonly found in third party libraries usually have the following signature: is(type).
In this fashion, you could write:
square.prototype.is = function(type) {
switch (String(type).toLowerCase()) {
case "king": return this.piece === "K";
case "queen": return this.piece === "Q";
default: return false;
}
}
Assuming type would be a String. But it can an Number defined in an Object to act as a Enum.
I hope you won't consider this off-topic as it can be a solution to consider alternative ways :)
I know this does not answer directly to your question, but the is functions that are commonly found in third party libraries usually have the following signature: is(type).
In this fashion, you could write:
square.prototype.is = function(type) {
switch (String(type).toLowerCase()) {
case "king": return this.piece === "K";
case "queen": return this.piece === "Q";
default: return false;
}
}
Assuming type would be a String. But it can an Number defined in an Object to act as a Enum.
I hope you won't consider this off-topic as it can be a solution to consider alternative ways :)
edited Nov 24 '18 at 17:46
answered Nov 24 '18 at 13:36
ZimZim
1,0741817
1,0741817
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
add a comment |
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
1
1
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
+1, this is a much better solution than what was asked for :-)
– Bergi
Nov 24 '18 at 14:32
add a comment |
1
What does
thisrefer to?thisrefering to a Chess instance orthisreferring to some other object?– kemicofa
Nov 24 '18 at 13:04
I edited my post, the class in questions is named
SquarenotChess. All thethiskeywords refer to aSquareinstance– David
Nov 24 '18 at 13:15
What if you return
thison both king and queen fns– enapupe
Nov 24 '18 at 13:20
Sorry, I don't quite get what you mean. You mean the methods in the first code block? The problem is going from Square.prototype.isKing = function() { return this.piece === "K"} to Square.prototype.is = { king: function() { return this.piece === "K" } } changes what the
thiskey refers to. In the first example it refers to a Square instance, in the second it refers to the Square.prototype.is object. I might have answered your first question wrong, sorry for the confussion– David
Nov 24 '18 at 13:29