如何在fabric.util类结构版本2中扩展成员(Property)?

时间:2018-08-03 05:59:53

标签: javascript fabricjs

我必须在新版本中使用fabric.util.createAccessors()。因此,我必须扩展此成员函数。

/**
 * Creates accessors (getXXX, setXXX) for a "class", based on "stateProperties" array
 * @static
 * @memberOf fabric.util
 * @param {Object} klass "Class" to create accessors for
 */
createAccessors: function(klass) {
  var proto = klass.prototype, i, propName,
      capitalizedPropName, setterName, getterName;

  for (i = proto.stateProperties.length; i--; ) {

    propName = proto.stateProperties[i];
    capitalizedPropName = propName.charAt(0).toUpperCase() + propName.slice(1);
    setterName = 'set' + capitalizedPropName;
    getterName = 'get' + capitalizedPropName;

    // using `new Function` for better introspection
    if (!proto[getterName]) {
      proto[getterName] = (function(property) {
        return new Function('return this.get("' + property + '")');
      })(propName);
    }
    if (!proto[setterName]) {
      proto[setterName] = (function(property) {
        return new Function('value', 'return this.set("' + property + '", value)');
      })(propName);
    }
  }
},

1 个答案:

答案 0 :(得分:1)

转到build,在Named accessors上打钩,然后下载自定义版本。

否则你可以做

fabric.util.createAccessors = function(klass){
 //.....
}