在 JavaScript 中创建面向对象编程的本地状态
我对 javascript 比较陌生,并且观看了 Douglas Crockford 关于该主题的两个教程视频。他建议通过使用无名函数和闭包,通过以下方式在 javascript 中创建面向对象的设计:
function Class() {
var privateVar1,
privateVar2;
function privateMethod1() {
}
function privateMethod2() {
}
return {
var publicVar1,
publicVar2;
publicMethod1: function () {
}
publicMethod2: function() {
}
};
}
这里的麻烦是当我创建一个新类时,例如
var a = Class();
当我尝试使用我在我在类定义中返回的对象文字。更具体地说,我收到错误 Class.publicMethod1 不是函数。谁能看出这里出了什么问题吗?我一定在这里遗漏了一些东西,因为道格拉斯·克罗克福德肯定不会在这件事上如此公然地错误。
编辑: 当我发布该代码片段时已是深夜,我犯了一些语法错误。抱歉浪费了您的时间。这是我遇到问题的实际代码片段。
return {
//public methods
getNextMoveValues: function(board, player) {
currentBoard = board;
if(isBoardValid()) {
var completeURL = assembleString(board, player);
return queryServer(completeURL);
} else {
console.err("board arg not valid in MoveGenerator::getNextMoveValues");
}
},
toString: function () {
var rtn = "currentPlayer: " + currentPlayer + "\t" + "currentBoard: " +
currentBoard + "\t" + "gameOption:" + gameOption + "\n";
return rtn;
}
};
当我运行代码时,执行以下命令时得到的唯一错误是“moveGen.getNextMoveValues(STARTING_BOARD, true) 未定义”: console.log(moveGen.getNextMoveValues(STARTING_BOARD, true).response);
I am relatively new to javascript and I have watched two tutorial videos by Douglas Crockford on the subject. He recommends creating object oriented design in javascript in the following way, through the use of nameless functions and closures:
function Class() {
var privateVar1,
privateVar2;
function privateMethod1() {
}
function privateMethod2() {
}
return {
var publicVar1,
publicVar2;
publicMethod1: function () {
}
publicMethod2: function() {
}
};
}
The trouble here is when I make a new class, such as
var a = Class();
I get errors when I attempt to use the public methods I declared in the object literal I'm returning in the class definition. More specifically, I get the error Class.publicMethod1 is not a function. Can anyone see what's wrong here? I must be missing something here bc surely Douglas Crockford can't be this blatantly incorrect about this.
EDIT:
It was late at night when I was posting that code snippet and I made some syntactical errors. Sorry to have wasted your time. This is a snippet of the actual code that I am having trouble with.
return {
//public methods
getNextMoveValues: function(board, player) {
currentBoard = board;
if(isBoardValid()) {
var completeURL = assembleString(board, player);
return queryServer(completeURL);
} else {
console.err("board arg not valid in MoveGenerator::getNextMoveValues");
}
},
toString: function () {
var rtn = "currentPlayer: " + currentPlayer + "\t" + "currentBoard: " +
currentBoard + "\t" + "gameOption:" + gameOption + "\n";
return rtn;
}
};
When I run the code, the only error I get is "moveGen.getNextMoveValues(STARTING_BOARD, true) is undefined" when I execute the following command:
console.log(moveGen.getNextMoveValues(STARTING_BOARD, true).response);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的语法无效:
应该是
您在这里使用对象文字,其形式始终为:
其中
value
也可以省略:{member, member: value}
读取有关对象初始值设定项的更多信息。
Your syntax is invalid:
should be
You are using an object literal here, which always has the form:
where
value
can also be omitted:{member, member: value}
Read more about object initializers.
关键是
privateVar*
和privateFunctions*
不是刚刚返回的对象的成员。它们是只能由返回对象访问的变量和/或函数(闭包)。只有返回的对象才能访问定义了privateVar*
和privateFunctions*
的范围。The point is that
privateVar*
andprivateFunctions*
are not members of just-returned object. They are variables and/or functions (closures) that can be accessed only by returned object. Only returned object can access scope whereprivateVar*
andprivateFunctions*
are defined.您需要了解对象文字表示法 。
You need to understand the object literal notation.