zoukankan      html  css  js  c++  java
  • enyo学习之enyo.Object类

         enyo.Object类是Enyo的基类,它实现了框架属性的一些公共方法。官方API文档给出了该类中几个方法的名称、参数和作用。

    : function(inName)  销毁名为inName的对象

    : function(n)  返回名称为‘n’的属性的值

    : function(n, v)  将名称为‘n’的属性赋值为‘v’

    : function()  向控制台发送log信息

    : function()  和log类似,但是使用的是控制台的warn方法(如果存在该方法)

    : function() 和log类似,但是使用的是控制台的error方法(如果存在该方法)

        接下来简单看一下enyo.Object类的源码

      1 enyo.kind({
      2     name: "enyo.Object",
      3     //* @protected
      4     // kind为空,没有父类
      5     kind: null,
      6     constructor: function() {
      7         enyo._objectCount++;
      8     },
      9     /**
     10         将名称为‘n’的属性值设置为‘v’,如果有回调方法cf则调用回调方法并将原来的值作为参数。
     11         所有属性的set方法都阻塞在这里以便对象观察属性的变化情况
     12     */
     13     setPropertyValue: function(n, v, cf) {
     14         if (this[cf]) {
     15             var old = this[n];
     16             this[n] = v;
     17             this[cf](old);
     18         } else {
     19             this[n] = v;
     20         }
     21     },
     22     _setProperty: function(n, v, cf) {
     23         this.setPropertyValue(n, v, (this.getProperty(n) !== v) && cf);
     24     },
     25     //* @public
     26     destroyObject: function(inName) {
     27         if (this[inName] && this[inName].destroy) {
     28             this[inName].destroy();
     29         }
     30         this[inName] = null;
     31     },
     32     getProperty: function(n) {
     33         var getter = "get" + enyo.cap(n);
     34         if (this[getter]) {
     35             return this[getter]();
     36         }
     37         return this[n];
     38     },
     39     /**
     40         设置属性值,如果该属性由set方法则调用set方法,否则调用_setProperty方法,并使用属性的Changed方法作为回调函数
     41     */
     42     setProperty: function(n, v) {
     43         var setter = "set" + enyo.cap(n);
     44         if (this[setter]) {
     45             this[setter](v);
     46         } else {
     47             this._setProperty(n, v, n + "Changed");
     48         }
     49     },
     50     /**
     51         Sends a log message to the console, prepended with the name of the kind
     52         and method from which _log_ was invoked.  Multiple arguments are coerced
     53         to String and joined with spaces.
     54 
     55             enyo.kind({
     56                 name: "MyObject",
     57                 kind: enyo.Object,
     58                 hello: function() {
     59                     this.log("says", "hi");
     60                     // shows in the console: MyObject.hello: says hi
     61                 }
     62             });
     63     */
     64     log: function() {
     65         var acc = arguments.callee.caller;
     66         var nom = ((acc ? acc.nom : "") || "(instance method)") + ":";
     67         enyo.logging.log("log", [nom].concat(enyo.cloneArray(arguments)));
     68     },
     69     //* Same as _log_, except uses the console's warn method (if it exists).
     70     warn: function() {
     71         this._log("warn", arguments);
     72     },
     73     //* Same as _log_, except uses the console's error method (if it exists).
     74     error: function() {
     75         this._log("error", arguments);
     76     },
     77     //* @protected
     78     _log: function(inMethod, inArgs) {
     79         if (enyo.logging.shouldLog(inMethod)) {
     80             try {
     81                 throw new Error();
     82             } catch(x) {
     83                 enyo.logging._log(inMethod, [inArgs.callee.caller.nom + ": "].concat(enyo.cloneArray(inArgs)));
     84                 console.log(x.stack);
     85             }
     86         }
     87     }
     88 });
     89 
     90 //* @protected
     91 //对象计数器
     92 enyo._objectCount = 0;
     93 
     94 enyo.Object.subclass = function(ctor, props) {
     95     this.publish(ctor, props);
     96 };
     97 
     98 /**
     99     为对象的publish属性添加get、set方法
    100 */
    101 enyo.Object.publish = function(ctor, props) {
    102     var pp = props.published;
    103     if (pp) {
    104         var cp = ctor.prototype;
    105         for (var n in pp) {
    106             enyo.Object.addGetterSetter(n, pp[n], cp);
    107         }
    108     }
    109 };
    110 
    111 /**
    112     为对象的属性添加get、set方法的实现代码,注意添加set方法时将Changed方法作为了回调函数
    113 */
    114 enyo.Object.addGetterSetter = function(inName, inValue, inProto) {
    115     var priv_n = inName;
    116     inProto[priv_n] = inValue;
    117     //
    118     var cap_n = enyo.cap(priv_n); 
    119     var get_n = "get" + cap_n;
    120     if (!inProto[get_n]) {
    121         inProto[get_n] = function() { 
    122             return this[priv_n];
    123         };
    124     }
    125     //
    126     var set_n = "set" + cap_n;
    127     var change_n = priv_n + "Changed";
    128     if (!inProto[set_n]) {
    129         inProto[set_n] = function(v) { 
    130             this._setProperty(priv_n, v, change_n); 
    131         };
    132     }
    133 };

          JavaScript没有深入系统的学过,只能简单的理解一下。

  • 相关阅读:
    kfx格式的复活
    HTC(HTML Component)开发简介
    window.open window.showModelDialog 打开一个新窗口/子窗口中调用父窗口的方法
    用 Firebug 动态调试和优化应用程序
    HTC——浏览器上的舞者
    innerHTML、innerText和outerHTML、outerText的区别
    window.parent与window.opener的区别与使用
    window.parent与window.opener、window.showModalDialog的区别 opener和showModalDialog刷新父页面的方法
    HTML Component(HTC)
    Hibernate下数据批量处理解决方案
  • 原文地址:https://www.cnblogs.com/waimai/p/2850086.html
Copyright © 2011-2022 走看看