jsb属性绑定的细节

1 阅读1分钟
const info = this.dbArmature.playAnimation(ani, 0);
console.log('duration:', info._duration)
console.log('totalTime:', info.totalTime);

totalTime在c++未找到属性绑定是函数,但是却有绑定的函数

    cls->defineFunction("getTotalTime", _SE(js_cocos2dx_dragonbones_AnimationState_getTotalTime));
  • jsb-adapter/jsb-builin.js中有这样的适配逻辑
jsb.generateGetSet = function (moduleObj) {
  for (var classKey in moduleObj) {
    var classProto = moduleObj[classKey] && moduleObj[classKey].prototype;
    if (!classProto) continue;

    var _loop = function _loop(getName) {
      var getPos = getName.search(/^get/);
      if (getPos == -1) return "continue";
      var propName = getName.replace(/^get/, '');
      var nameArr = propName.split('');
      var lowerFirst = nameArr[0].toLowerCase();
      var upperFirst = nameArr[0].toUpperCase();
      nameArr.splice(0, 1);
      var left = nameArr.join('');
      propName = lowerFirst + left;
      var setName = 'set' + upperFirst + left;// 如果有get set对应的函数,就会绑定到jsb中
      if (classProto.hasOwnProperty(propName)) return "continue";
      var setFunc = classProto[setName];
      var hasSetFunc = typeof setFunc === 'function';

      if (hasSetFunc) {
        Object.defineProperty(classProto, propName, {
          get: function get() {
            return this[getName]();
          },
          set: function set(val) {
            this[setName](val);
          },
          configurable: true
        });
      } else {
        Object.defineProperty(classProto, propName, {
          get: function get() {
            return this[getName]();
          },
          configurable: true
        });
      }
    };

    for (var getName in classProto) {
      var _ret = _loop(getName);

      if (_ret === "continue") continue;
    }
  }
};