letObserver = (() => { let _message = {} return { regist: function (type, fn) { if (typeof _message[type] === "undefined") { _message[type] = [fn] } else { _message[type].push(fn) } }, fire: function (type, args) { if (!_message[type]) { return } let events = { type, args: args || {}, } for (let i = 0, len = _message[type].length; i < len; i++) { _message[type][i].call(this, events) } }, remove: function (type, fn) { if (_message[type] instanceofArray) { for (let i = _message[type].length - 1; i >= 0; i--) { _message[type][i] === fn && _message[type].splice(i, 1) } } }, } })()
Observer.regist("test", function (e) { // register console.log(e.type, e.args.msg) }) Observer.fire("test", { msg: "this is some test message" }) // send
letNamed = function (name) { let _this = this ;((name, _this) => { this.wholeName = name if (name.indexOf(" ") > -1) { _this.FirstName = name.slice(0, name.indexOf(" ")) _this.LastName = name.slice(name.indexOf(" ")) } })(name, _this) }
letWork = function (work) { let _this = this ;((work, _this) => { switch (work) { case"code": this.work = "developer" break case"design": this.work = "designer" break default: this.work = work } })(work, _this) }
Work.prototype.changeWork = function (work) { this.work = work }
/** * to create a Person * @param {*} name * @param {*} work */ letPerson = function (name, work) { let _person = newHuman() _person.name = newNamed(name) _person.work = newWork(work) return _person }
// for test let person = newPerson("Jack Edward", "design") console.log(person.work) console.log(person.name) person.work.changeWork("code") console.log(person.work)