在 JavaScript 中创建面向对象编程的本地状态

发布于 2024-09-27 11:15:01 字数 1347 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山有枢 2024-10-04 11:15:01

您的语法无效:

 return {
  var publicVar1,
   publicVar2;

  publicMethod1: function () {
  }

  publicMethod2: function() {
  }
 }; 

应该是

 return {
  publicVar1,
  publicVar2,  
  publicMethod1: function () {},
  publicMethod2: function() {}
 }; 

您在这里使用对象文字,其形式始终为:

{member: value, member: value}

其中 value 也可以省略: {member, member: value}


读取有关对象初始值设定项的更多信息。

Your syntax is invalid:

 return {
  var publicVar1,
   publicVar2;

  publicMethod1: function () {
  }

  publicMethod2: function() {
  }
 }; 

should be

 return {
  publicVar1,
  publicVar2,  
  publicMethod1: function () {},
  publicMethod2: function() {}
 }; 

You are using an object literal here, which always has the form:

{member: value, member: value}

where value can also be omitted: {member, member: value}


Read more about object initializers.

浅暮の光 2024-10-04 11:15:01
function Class(arg1, arg2) {
  var privateVar1 = arg1,
      privateVar2;

  function privateFunction1() {
    // use arg2
  }

  function privateFunction2() { 
  }

  return {
    publicVar1: 'something',
    publicVar2: 'something',
    publicMethod1: function () { },
    publicMethod2: function() { }
  }
}

关键是 privateVar*privateFunctions* 不是刚刚返回的对象的成员。它们是只能由返回对象访问的变量和/或函数(闭包)。只有返回的对象才能访问定义了 privateVar*privateFunctions* 的范围。

function Class(arg1, arg2) {
  var privateVar1 = arg1,
      privateVar2;

  function privateFunction1() {
    // use arg2
  }

  function privateFunction2() { 
  }

  return {
    publicVar1: 'something',
    publicVar2: 'something',
    publicMethod1: function () { },
    publicMethod2: function() { }
  }
}

The point is that privateVar* and privateFunctions* 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 where privateVar* and privateFunctions* are defined.

半透明的墙 2024-10-04 11:15:01

您需要了解对象文字表示法

function Class() {

    var privateVar1, privateVar2;

    function privateMethod1() {}
    function privateMethod2() {}

    return {                           // {
        publicVar1: "value",           // name: value,
        publicVar2: 5,                 // name: value,
        publicMethod1: function () {}, // name: value,
        publicMethod2: function () {}  // name: value (no comma at the end!)
    };                                 // }
}

You need to understand the object literal notation.

function Class() {

    var privateVar1, privateVar2;

    function privateMethod1() {}
    function privateMethod2() {}

    return {                           // {
        publicVar1: "value",           // name: value,
        publicVar2: 5,                 // name: value,
        publicMethod1: function () {}, // name: value,
        publicMethod2: function () {}  // name: value (no comma at the end!)
    };                                 // }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文