platform下的js分析_3

主要包含 CCSAXParser.js

CCSAXParser.js

主要用于 解析 xml
如: cocos2d/tilemap/CCTMXXMLParser.js 文件中解析 tilemap的方法.
具体解析方式见 其中的 parseXMLString (xmlStr, tilesetFirstGid) 方法.

preprocess-class.js

增加预处理属性这个步骤的目的是降低 CCClass 的实现难度,将比较稳定的通用逻辑和一些需求比较灵活的属性需求分隔开。
主要方法是 preprocessAttrs(properties, className, cls, es6) 这个方法.

  1. SerializableAttrs
    表示可序列话的属性

  2. parseNotify (val, propName, notify, properties)
    处理 properties 中, 属性为 propName 中的 notify 参数.
    参见 https://docs.cocos.com/creator/2.1/manual/zh/scripting/reference/attributes.html

  3. checkUrl (val, className, propName, url)
    检查 url 属性

  4. parseType (val, type, className, propName)
    检查 type 属性

  5. postCheckType (val, type, className, propName)
    type属性的后置 检查

  6. getFullFormOfProperty (options, propname_dev, classname_dev)
    获得 properties[propname_dev] 的描述. 是一个对象,包含各种属性参数.

  7. preprocessAttrs(properties, className, cls, es6)
    解析 CCClass() 参数中 properties属性时调用.

  8. validateMethodWithProps(func, funcName, className, cls, base)
    检查 CCClass() 参数中 extends properties statics ctor是否正确.

CCClass.js

这是creator里面比较复杂的一个类. 这里简单过一次处理过程.
处理流程如下:

function CCClass (options) {
    options = options || {};

    var name = options.name;
    var base = options.extends/* || CCObject*/;
    var mixins = options.mixins;

    // create constructor
    var cls = define(name, base, mixins, options);
    if (!name) {
        name = cc.js.getClassName(cls);
    }

    cls._sealed = true;
    if (base) {
        base._sealed = false;
    }

    // define Properties
    var properties = options.properties;
    if (typeof properties === 'function' ||
        (base && base.__props__ === null) ||
        (mixins && mixins.some(function (x) {
            return x.__props__ === null;
        }))
    ) {
        if (CC_DEV && options.__ES6__) {
            cc.error('not yet implement deferred properties for ES6 Classes');
        }
        else {
            deferredInitializer.push({cls: cls, props: properties, mixins: mixins});
            cls.__props__ = cls.__values__ = null;
        }
    }
    else {
        declareProperties(cls, name, properties, base, options.mixins, options.__ES6__);
    }

    // define statics
    var statics = options.statics;
    if (statics) {
        var staticPropName;
        if (CC_DEV) {
            for (staticPropName in statics) {
                if (INVALID_STATICS_DEV.indexOf(staticPropName) !== -1) {
                    cc.errorID(3642, name, staticPropName,
                        staticPropName);
                }
            }
        }
        for (staticPropName in statics) {
            cls[staticPropName] = statics[staticPropName];
        }
    }

    // define functions
    for (var funcName in options) {
        if (BUILTIN_ENTRIES.indexOf(funcName) >= 0) {
            continue;
        }
        var func = options[funcName];
        if (!preprocess.validateMethodWithProps(func, funcName, name, cls, base)) {
            continue;
        }
        // use value to redefine some super method defined as getter
        js.value(cls.prototype, funcName, func, true, true);
    }


    var editor = options.editor;
    if (editor) {
        if (js.isChildClassOf(base, cc.Component)) {
            cc.Component._registerEditorProps(cls, editor);
        }
        else if (CC_DEV) {
            cc.warnID(3623, name);
        }
    }

    return cls;
}
  1. define(name, base, mixins, options)
    此函数会调用 doDefine(className, baseClass, mixins, options) 函数,
    返回我们创建的 类的 constructor, 记作 cls.

  2. declareProperties(cls, name, properties, base, options.mixins, options.__ES6__)
    此函数会使用 preprocess-class.js 里面的 preprocessAttrs(properties, className, cls, es6)方法,
    对 properties 进行处理. 然后将 properties 里面的属性 放到 cls.__props__ 数组里.

  3. 处理 statics 变量和函数.

  4. 处理普通 属性和函数.
    注意,这里面处理的普通属性是放到 cls.prototype 上.

  5. 处理 editor
    参考: https://docs.cocos.com/creator/2.1/manual/zh/scripting/class.html

CCClass.js中 使用较多的函数.

  1. fastDefine
_fastDefine (className, constructor, serializableFields)

/*
此函数给 constructor 添加上__values__,__props__, __attrs__属性.
并且还会给 constructor.prototype 加上__classname__和__cid__ 属性.

Q1: 我们通过 cc.Class 创建的类,属性中__values__和__props__ 区别和用途.
__values__ 属性是 __props__ 的一个子集,__props__中包含所有的属性, __values__ 中包含所有可序列化的属性.
*/

转载于:https://www.cnblogs.com/daihanlong/p/10328638.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值