JS object factory and inherit sample
2014-05-16 07:16
846 查看
/* * Object factory */ function objectFactory(jsonObj){ function objectEntity(){ } if(typeof jsonObj == "object"){ for(var index in jsonObj){ objectEntity.prototype[index] = jsonObj[index]; } } return objectEntity; } var Person = objectFactory({ pname:'andy', sex:'man' }); var person = new Person(); console.info(person+"--"+Person);// [object Object] -- function objectEntity(){} console.info(person.pname); console.info(person.sex); <span></span>
<span>objectFactory create object per json obj-jsonObj create function objectEntity will check whether jsonObj is object, and iterate the json object, set attribute value to </span><span>objectEntity</span>
<span>return objectEntity, while Person refer to objectEntity</span>
/* * inherit */ function inherit(obj,prop){ function f(){ } if(typeof obj=="object"){ for(var index in obj){ f.prototype[index] = obj[index]; } }else{ f.prototype = obj.prototype; for(var index in prop){ f.prototype[index] = prop[index]; } } return f; } var Animal = inherit({ type:'animal', name:'animal', jump:'jump' }); var Dog = inherit(Animal,{ name:'i am a dog', jump:'dog jumpping' }); var dog = new Dog; console.info(dog.type); console.info(dog.name);
相关文章推荐
- JS object factory and inherit sample
- moss2007 webpart custom property sample (2) [a simple one, inherit from .net framework webpart]
- How to: Begin Sample with Serialization and Deserialization an Object
- How to load 3d model from different domain and display it with Three.js
- JS prototype and JS object
- c# dynamic,maybe I should say it is just some shortcut for "Object", box and unbox, without the cast
- Domain Object Layer Design and Sample Code for LiteMDA
- Atitit.js的键盘按键事件捆绑and事件调度
- AngularJS Service vs Factory - Once and for all
- Why we made vorlon.js and how to use it to debug your JavaScript remotely
- atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException v2 q31
- how to save a c++ object in java object and use it
- atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException
- JS - Object and Property的删除用法
- Atitit. Gui控件and面板----程序快速启动区--最佳实践Launchy ObjectDock-o0g
- Code Example 3: A ScriptEngineFactory object provides detailed information about the engine it provides.
- Getting Started with Mongoose and Node.js – A Sample Comments System | Dev Notes
- creating-skeletal-animation-in-blender-and-exporting-it-to-three-js
- inheritCombinedParasitic.js
- Atitit.js的键盘按键事件捆绑and事件调度