PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/1.4/api/source/object5.html

https://github.com/yiminghe/kissyteam.github.com
HTML | 373 lines | 335 code | 38 blank | 0 comment | 0 complexity | bd9aedb7e3632a2539e81c769c99e8c7 MD5 | raw file
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='global-property-'>/**
  19. </span> * @ignore
  20. * object utilities of lang
  21. * @author yiminghe@gmail.com
  22. *
  23. */
  24. (function (S, undefined) {
  25. var MIX_CIRCULAR_DETECTION = '__MIX_CIRCULAR',
  26. STAMP_MARKER = '__~ks_stamped',
  27. host = this,
  28. TRUE = true,
  29. EMPTY = '',
  30. Obj = Object,
  31. ObjectCreate = Obj.create,
  32. // error in native ie678, not in simulated ie9
  33. hasEnumBug = !({toString: 1}['propertyIsEnumerable']('toString')),
  34. enumProperties = [
  35. 'constructor',
  36. 'hasOwnProperty',
  37. 'isPrototypeOf',
  38. 'propertyIsEnumerable',
  39. 'toString',
  40. 'toLocaleString',
  41. 'valueOf'
  42. ];
  43. mix(S, {
  44. <span id='KISSY-method-stamp'> /**
  45. </span> * stamp a object by guid
  46. * @param {Object} o object needed to be stamped
  47. * @param {Boolean} [readOnly] while set marker on o if marker does not exist
  48. * @param {String} [marker] the marker will be set on Object
  49. * @return {String} guid associated with this object
  50. * @member KISSY
  51. */
  52. stamp: function (o, readOnly, marker) {
  53. marker = marker || STAMP_MARKER;
  54. var guid = o[marker];
  55. if (guid) {
  56. return guid;
  57. } else if (!readOnly) {
  58. try {
  59. guid = o[marker] = S.guid(marker);
  60. }
  61. catch (e) {
  62. guid = undefined;
  63. }
  64. }
  65. return guid;
  66. },
  67. <span id='KISSY-method-keys'> /**
  68. </span> * Get all the property names of o as array
  69. * @param {Object} o
  70. * @return {Array}
  71. * @member KISSY
  72. */
  73. keys: Obj.keys || function (o) {
  74. var result = [], p, i;
  75. for (p in o) {
  76. // S.keys(new XX())
  77. if (o.hasOwnProperty(p)) {
  78. result.push(p);
  79. }
  80. }
  81. if (hasEnumBug) {
  82. for (i = enumProperties.length - 1; i &gt;= 0; i--) {
  83. p = enumProperties[i];
  84. if (o.hasOwnProperty(p)) {
  85. result.push(p);
  86. }
  87. }
  88. }
  89. return result;
  90. },
  91. <span id='KISSY-method-mix'> /**
  92. </span> * Copies all the properties of s to r.
  93. * @method
  94. * @param {Object} r the augmented object
  95. * @param {Object} s the object need to augment
  96. * @param {Boolean|Object} [ov=TRUE] whether overwrite existing property or config.
  97. * @param {Boolean} [ov.overwrite=TRUE] whether overwrite existing property.
  98. * @param {String[]|Function} [ov.whitelist] array of white-list properties
  99. * @param {Boolean}[ov.deep=false] whether recursive mix if encounter object.
  100. * @param {String[]|Function} [wl] array of white-list properties
  101. * @param [deep=false] {Boolean} whether recursive mix if encounter object.
  102. * @return {Object} the augmented object
  103. * @member KISSY
  104. *
  105. * for example:
  106. * @example
  107. * var t = {};
  108. * S.mix({x: {y: 2, z: 4}}, {x: {y: 3, a: t}}, {deep: TRUE}) =&gt; {x: {y: 3, z: 4, a: {}}}, a !== t
  109. * S.mix({x: {y: 2, z: 4}}, {x: {y: 3, a: t}}, {deep: TRUE, overwrite: false}) =&gt; {x: {y: 2, z: 4, a: {}}}, a !== t
  110. * S.mix({x: {y: 2, z: 4}}, {x: {y: 3, a: t}}, 1) =&gt; {x: {y: 3, a: t}}
  111. */
  112. mix: function (r, s, ov, wl, deep) {
  113. if (typeof ov === 'object') {
  114. <span id='global-property-'> wl = /**
  115. </span> @ignore
  116. @type {String[]|Function}
  117. */ov['whitelist'];
  118. deep = ov['deep'];
  119. ov = ov['overwrite'];
  120. }
  121. if (wl &amp;&amp; (typeof wl !== 'function')) {
  122. var originalWl = wl;
  123. wl = function (name, val) {
  124. return S.inArray(name, originalWl) ? val : undefined;
  125. };
  126. }
  127. if (ov === undefined) {
  128. ov = TRUE;
  129. }
  130. var cache = [],
  131. c,
  132. i = 0;
  133. mixInternal(r, s, ov, wl, deep, cache);
  134. while (c = cache[i++]) {
  135. delete c[MIX_CIRCULAR_DETECTION];
  136. }
  137. return r;
  138. },
  139. <span id='KISSY-method-merge'> /**
  140. </span> * Returns a new object containing all of the properties of
  141. * all the supplied objects. The properties from later objects
  142. * will overwrite those in earlier objects. Passing in a
  143. * single object will create a shallow copy of it.
  144. * @param {...Object} var_args objects need to be merged
  145. * @return {Object} the new merged object
  146. * @member KISSY
  147. */
  148. merge: function (var_args) {
  149. var_args = S.makeArray(arguments);
  150. var o = {},
  151. i,
  152. l = var_args.length;
  153. for (i = 0; i &lt; l; i++) {
  154. S.mix(o, var_args[i]);
  155. }
  156. return o;
  157. },
  158. <span id='KISSY-method-augment'> /**
  159. </span> * Applies prototype properties from the supplier to the receiver.
  160. * @param {Object} r received object
  161. * @param {...Object} var_args object need to augment
  162. * {Boolean} [ov=TRUE] whether overwrite existing property
  163. * {String[]} [wl] array of white-list properties
  164. * @return {Object} the augmented object
  165. * @member KISSY
  166. */
  167. augment: function (r, var_args) {
  168. var args = S.makeArray(arguments),
  169. len = args.length - 2,
  170. i = 1,
  171. proto,
  172. arg,
  173. ov = args[len],
  174. wl = args[len + 1];
  175. if (!S.isArray(wl)) {
  176. ov = wl;
  177. wl = undefined;
  178. len++;
  179. }
  180. if (typeof ov !== 'boolean') {
  181. ov = undefined;
  182. len++;
  183. }
  184. for (; i &lt; len; i++) {
  185. arg = args[i];
  186. if (proto = arg.prototype) {
  187. arg = S.mix({}, proto, true, removeConstructor);
  188. }
  189. S.mix(r.prototype, arg, ov, wl);
  190. }
  191. return r;
  192. },
  193. <span id='KISSY-method-extend'> /**
  194. </span> * Utility to set up the prototype, constructor and superclass properties to
  195. * support an inheritance strategy that can chain constructors and methods.
  196. * Static members will not be inherited.
  197. * @param r {Function} the object to modify
  198. * @param s {Function} the object to inherit
  199. * @param {Object} [px] prototype properties to add/override
  200. * @param {Object} [sx] static properties to add/override
  201. * @return r {Object}
  202. * @member KISSY
  203. */
  204. extend: function (r, s, px, sx) {
  205. if (!s || !r) {
  206. return r;
  207. }
  208. var sp = s.prototype,
  209. rp;
  210. // in case parent does not set constructor
  211. // eg: parent.prototype={};
  212. sp.constructor = s;
  213. // add prototype chain
  214. rp = createObject(sp, r);
  215. r.prototype = S.mix(rp, r.prototype);
  216. r.superclass = sp;
  217. // add prototype overrides
  218. if (px) {
  219. S.mix(rp, px);
  220. }
  221. // add object overrides
  222. if (sx) {
  223. S.mix(r, sx);
  224. }
  225. return r;
  226. },
  227. <span id='KISSY-method-namespace'> /**
  228. </span> * Returns the namespace specified and creates it if it doesn't exist. Be careful
  229. * when naming packages. Reserved words may work in some browsers and not others.
  230. *
  231. * for example:
  232. * @example
  233. * S.namespace('KISSY.app'); // returns KISSY.app
  234. * S.namespace('app.Shop'); // returns KISSY.app.Shop
  235. * S.namespace('TB.app.Shop', TRUE); // returns TB.app.Shop
  236. *
  237. * @return {Object} A reference to the last namespace object created
  238. * @member KISSY
  239. */
  240. namespace: function () {
  241. var args = S.makeArray(arguments),
  242. l = args.length,
  243. o = null, i, j, p,
  244. global = (args[l - 1] === TRUE &amp;&amp; l--);
  245. for (i = 0; i &lt; l; i++) {
  246. p = (EMPTY + args[i]).split('.');
  247. o = global ? host : this;
  248. for (j = (host[p[0]] === o) ? 1 : 0; j &lt; p.length; ++j) {
  249. o = o[p[j]] = o[p[j]] || { };
  250. }
  251. }
  252. return o;
  253. }
  254. });
  255. function Empty() {
  256. }
  257. function createObject(proto, constructor) {
  258. var newProto;
  259. if (ObjectCreate) {
  260. newProto = ObjectCreate(proto);
  261. } else {
  262. Empty.prototype = proto;
  263. newProto = new Empty();
  264. }
  265. newProto.constructor = constructor;
  266. return newProto;
  267. }
  268. function mix(r, s) {
  269. for (var i in s) {
  270. r[i] = s[i];
  271. }
  272. }
  273. function mixInternal(r, s, ov, wl, deep, cache) {
  274. if (!s || !r) {
  275. return r;
  276. }
  277. var i, p, keys, len;
  278. // 记录循环标志
  279. s[MIX_CIRCULAR_DETECTION] = r;
  280. // 记录被记录了循环标志的对像
  281. cache.push(s);
  282. // mix all properties
  283. keys = S.keys(s);
  284. len = keys.length;
  285. for (i = 0; i &lt; len; i++) {
  286. p = keys[i];
  287. if (p != MIX_CIRCULAR_DETECTION) {
  288. // no hasOwnProperty judge!
  289. _mix(p, r, s, ov, wl, deep, cache);
  290. }
  291. }
  292. return r;
  293. }
  294. function removeConstructor(k, v) {
  295. return k == 'constructor' ? undefined : v;
  296. }
  297. function _mix(p, r, s, ov, wl, deep, cache) {
  298. // 要求覆盖
  299. // 或者目的不存在
  300. // 或者深度mix
  301. if (ov || !(p in r) || deep) {
  302. var target = r[p],
  303. src = s[p];
  304. // prevent never-end loop
  305. if (target === src) {
  306. // S.mix({},{x:undefined})
  307. if (target === undefined) {
  308. r[p] = target;
  309. }
  310. return;
  311. }
  312. if (wl) {
  313. src = wl.call(s, p, src);
  314. }
  315. // 来源是数组和对象,并且要求深度 mix
  316. if (deep &amp;&amp; src &amp;&amp; (S.isArray(src) || S.isPlainObject(src))) {
  317. if (src[MIX_CIRCULAR_DETECTION]) {
  318. r[p] = src[MIX_CIRCULAR_DETECTION];
  319. } else {
  320. // 目标值为对象或数组,直接 mix
  321. // 否则 新建一个和源值类型一样的空数组/对象,递归 mix
  322. var clone = target &amp;&amp; (S.isArray(target) || S.isPlainObject(target)) ?
  323. target :
  324. (S.isArray(src) ? [] : {});
  325. r[p] = clone;
  326. mixInternal(clone, src, ov, wl, TRUE, cache);
  327. }
  328. } else if (src !== undefined &amp;&amp; (ov || !(p in r))) {
  329. r[p] = src;
  330. }
  331. }
  332. }
  333. })(KISSY);</pre>
  334. </body>
  335. </html>