2

I'm working on a new OOP model for JavaScript and I'm wondering whether you consider it right to make methods on objects enumerable or only the data members. I can see some sense in both and maybe there is no definite answer.

Can also make the own methods enumerable and the inherited ones not...

That said I feel it makes sense anyways to make all data members enumerable even if they are inherited.

update: this seemed not clear from what people are answering. I am creating a OOP model which will allow users to write something like this to declare a class:

update 2: in the mean time the project is out and about, this is what it has become: OoJs. In it, user defined properties including methods are enumerable, properties added by the framework aren't.

;(function( namespace )
{
   'use strict';

       namespace.Shape = Shape
   var Static          = namespace.OoJs.setupClass( "Shape" )


   // Data members
   //
   Static.canvas = null


   Static.Protected( "canvas" )  // Protected members

   Static.Public   ()            // Public members


   // constructor
   //
   function Shape()
   {
      // Data members
      //
      this.sides = null

      // Private methods
      //
      this.init  = init


      this.Protected( "sides" )               // Protected members


      var iFace = this.Public( getOffset )    // Public interface


      this.init() // for example      


      return iFace
   }


   // Method definitions
   //
   function init     (){ /*do something useful*/   }
   function getOffset(){ return [ this.x, this.y ] }

})( window )

So the question is if you would use this to declare your classes, would you assume/want methods to be enumerable or not or should there be a way to configure either classwide or per member whether it should be enumerable not?

4

2 回答 2

0

经过考虑,Felix Kling 的评论引出了答案。

因为这取决于具体情况,而且由于我不了解用户的情况,所以我不应该限制他们的选择,至少提供与原生对象模型一样丰富的功能集。我将允许用户在其属性上设置类似于Object.defineProperty的选项。

我认为保持用户的选项开放应该被认为是一个重要的编程原则,所以这不是一个艰难的决定。

问题仍然在于默认值应该是什么。在标准 javascript 中,添加到原型的属性都是可枚举的。

于 2013-08-05T22:43:42.837 回答
0

除非有一些与键相关的语义——你鼓励你的对象的用户迭代它的属性——那么让方法是可枚举的既没有优点也没有缺点。

隐藏它们你什么也得不到,而且解释或部分评估你的脚本的 IDE 和其他工具总是可以getOwnPropertyNames用来提供自动完成等。

Object.getOwnPropertyNames

返回直接在给定对象上找到的所有属性(可枚举或不可枚举)的数组。

于 2013-08-05T18:04:02.760 回答