object name same a function name?

前端 未结 6 1583
余生分开走
余生分开走 2020-12-17 00:35

If We have

var randomname = {};
randomname.attribute = \'something\';

function randomname(){
  alert(randomname.attribute);
}
randomname();
6条回答
  •  余生分开走
    2020-12-17 00:53

    I know this is an old post, but I found an example of how it works.

    In fact, I believe sequelize.js does it with the following code:

    var STRING = function(length, binary) {
      if (this instanceof STRING) {
        this._binary = !!binary
        if (typeof length === 'number') {
          this._length = length
        } else {
          this._length = 255
        }
      } else {
        return new STRING(length, binary)
      }
    }
    
    STRING.prototype = {
      get BINARY() {
        this._binary = true
        return this
      },
      get type() {
        return this.toString()
      },
      toString: function() {
        return 'VARCHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : '')
      }
    }
    
    Object.defineProperty(STRING, 'BINARY', {
      get: function() {
        return new STRING(undefined, true)
      }
    })
    

    You can see it in their docs for data types: http://sequelizejs.com/docs/latest/models#block-2-line-0. Notice Sequelize.STRING and Sequelize.STRING(1234)

提交回复
热议问题