Javascript类方法声明引发令牌错误
我在定义类方法时遇到问题:Javascript类方法声明引发令牌错误,javascript,es6-class,Javascript,Es6 Class,我在定义类方法时遇到问题: this.method = function(){...} 我在“this”之后的“.”处出错 如果直接使用method(){…}声明该方法,则无法在其他方法中引用它,因为它表明该方法未定义 我要定义的方法是shuffleBoard()。我该怎么做 class Board { constructor(size,boardId){ this.boardId = boardId; switch(size){ ca
this.method = function(){...}
我在“this”之后的“.”处出错
如果直接使用method(){…}
声明该方法,则无法在其他方法中引用它,因为它表明该方法未定义
我要定义的方法是shuffleBoard()
。我该怎么做
class Board {
constructor(size,boardId){
this.boardId = boardId;
switch(size){
case "medium":
var boardSize = 560;
var tiles = 7*7;
break;
case "large":
boardSize = 720;
tiles = 9*9;
break;
default:
boardSize = 320;
tiles = 4*4;
break;
}
var container = $(this.boardId+" .tiles-container");
var row = 0;
var loopArray = [];
for(var i = 0;i < tiles; i++){
var tile = document.createElement("div");
loopArray.push(i);
var text = i+1;
tile.setAttribute("index",i+1);
tile.id = i+1;
if(i == tiles - 1){
var empty = "empty"
}
tile.setAttribute("class","tile "+empty);
tile.innerText = text;
container.append(tile);
(function(){
tile.onclick = function(){
var tileObject = new Tile(this.getAttribute("index"));
console.log(tileObject.move());
}
})()
var prevRow = row;
if(i%4 == 0 && i != 0){
row++
}
if(row > prevRow){
var positionX = 0;
}
else{
var positionX = (i%4)*80;
}
var positionY = row*80;
tile.style.top=positionY+"px";
tile.style.left=positionX+"px";
console.log(i+"---"+row+"////"+prevRow);
}
setTimeout(function(){this.shuffleBoard(loopArray);},4000);
return container;
}
this.shuffleBoard = function(arr){
var i = 0;
console.log(this.boardId);
$(this.boardId+" .tiles-container tile").forEach(function(el){
var shuffled = shuffle(arr);
el.innerText = shuffled[i];
arr.pop(arr[i]);
i++
});
}
}
课程板{
构造函数(大小、boardId){
this.boardId=boardId;
开关(尺寸){
案例“中等”:
var boardSize=560;
var-tiles=7*7;
打破
案例“大”:
板尺寸=720;
瓷砖=9*9;
打破
违约:
木板尺寸=320;
瓷砖=4*4;
打破
}
var容器=$(this.boardId+“.tiles容器”);
var行=0;
var loopArray=[];
对于(变量i=0;i上一行){
变量x=0;
}
否则{
var位置X=(i%4)*80;
}
变量位置Y=行*80;
tile.style.top=positionY+“px”;
tile.style.left=位置X+“px”;
console.log(i+“--”+行+“///”+前行);
}
setTimeout(函数(){this.shuffleBoard(loopArray);},4000);
返回容器;
}
this.shuffleBoard=函数(arr){
var i=0;
console.log(this.boardId);
$(this.boardId+“.tiles容器tile”).forEach(函数(el){
var-shuffled=随机(arr);
el.innerText=shuffled[i];
arr.pop(arr[i]);
我++
});
}
}
您似乎在使用ES6语法。在ES6中,编写如下函数
shuffleBoard() {
// rest of the code
}
要访问它,请使用这个
关键字。像这个.shuffleBoard()
要在setTimeout中调用它,请使用箭头函数
setTimeout(() => { this.shuffleBoard(loopArray); }, 4000);
看起来您正在使用ES6语法。在ES6中,编写如下函数
shuffleBoard() {
// rest of the code
}
要访问它,请使用这个
关键字。像这个.shuffleBoard()
要在setTimeout中调用它,请使用箭头函数
setTimeout(() => { this.shuffleBoard(loopArray); }, 4000);
然后,您可以使用以下方法:
let board = new Board()
board.shuffleBoard()
然后,您可以使用以下方法:
let board = new Board()
board.shuffleBoard()
1.必须使用箭头函数来保持作用域,否则
将指向超时中创建的新函数
setTimeout(() => {
this.shuffleBoard(loopArray);
}, 4000);
2.构造函数不能返回任何内容,因为它阻止它返回它构造的对象
3.jQuery使用.each()
迭代jQuery对象,而不是.forEach()
我将注释直接放在代码中作为注释:
课程板{
构造函数(大小、boardId){
this.boardId=boardId;
开关(尺寸){
案例“中等”:
var boardSize=560;
var-tiles=7*7;
打破
案例“大”:
板尺寸=720;
瓷砖=9*9;
打破
违约:
木板尺寸=320;
瓷砖=4*4;
打破
}
var容器=$(this.boardId+“.tiles容器”);
var行=0;
var loopArray=[];
对于(变量i=0;i上一行){
变量x=0;
}否则{
var位置X=(i%4)*80;
}
变量位置Y=行*80;
tile.style.top=位置Y+“px”;
tile.style.left=位置X+“px”;
console.log(i+“--”+行+“///”+前行);
}
setTimeout(()=>{//使用箭头函数保留作用域
这个.shuffleBoard(loopArray);
}, 4000);
//返回容器;在此处返回容器可防止构造函数返回构造的对象
}
沙狐牌(arr){
var i=0;
console.log(this.boardId);
$(this.boardId+“.tiles容器tile”)。每个(函数(el){//jQuery使用.each而不是forEach
var-shuffled=随机(arr);
el.innerText=shuffled[i];
arr.pop(arr[i]);
我++
});
}
}
let board=新董事会(“中”、“我的董事会”);
控制台。日志(板。沙狐板);
董事会。沙狐牌([])代码>
1.必须使用箭头函数来保持作用域,否则将指向超时中创建的新函数
setTimeout(() => {
this.shuffleBoard(loopArray);
}, 4000);
2.构造函数不能返回任何内容,因为它阻止它返回它构造的对象
3.jQuery使用.each()
迭代jQuery对象,而不是.forEach()
我将注释直接放在代码中作为注释:
课程板{
构造函数(大小、boardId){
this.boardId=boardId;
开关(尺寸){
案例“中等”:
野猪