我正在尝试学习在 JavaScript 中进行面向对象的编程并严格违反 JSLint。我知道我在非全局环境中使用它(或类似的东西......),但我不知道如何正确地做到这一点。这是我的代码:
function piece(color, type, x, y, captured, hasMoved) {
"use strict";
this.color = color;
this.type = type;
this.x = x;
this.y = y;
this.captured = captured;
this.hasMoved = hasMoved;
this.movePiece = movePiece;
function movePiece(x, y) {
// if(isLegal(x, y, this.type){
// this.x = x;
// this.y = y;
// }
alert("you moved me!");
}
}
var whitePawn1 = piece("white", "pawn", 0, 1, false, false);
var blackBishop1 = piece("black", "bishop", 8, 3, false, false);