function Person(name,age){
this.name = name
this.age = age
}
Person.prototype.show = function(){
console.log('My name is'+this.name,'My age is '+this.age)
}
var p1 = new Person('Lii',18)
console.log(p1.show())
var Person = function(name,age){
this.name = name
this.age = age
}
Person.prototype.sayHi=function(){
console.log(this.name, this.age)
}
var p1 = new Person('Lii',18)
p1.sayHi()
var Person = (function(){
var name = 'xxx'
var sayName = function(){
console.log(name)
}
return {
name:name,
sayName:sayName
}
})()
var p1 = Person
p1.sayName()
function person(name,age){
var obj = {}
obj.name = name
obj.age = age
return obj
}
var p1 = person('Lii',18)
cosole.log(p1.name,p1.age)
var Person = !function(){
var instance;
function init(){
return {
}
}
return {
createPerson:function(){
if(!instance){
instance = init()
}
return instace
}
}
}()
var p1 = Person.createPerson()
var p2 = Person.createPerson()
var xxx = (function(){
var obj = {}//自定义事件存放
var on = function(evt,handler){//push到数组
obj[evt] = obj[evt] || [];
obj[evt].push({
handler:handler
})
}
var fire = function(evt,args){//形成映射查找
if(obj[!evt]) return;
for(var i=0;i<obj[evt].length;i++){
obj[evt][i].handler(args)
}
}
return {
on: on,
fire: fire
}
})()
xxx.on('m-coco',function(data){
console.log('m-coco1')
})
xxx.on('m-coco',function(data){
console.log('m-coco2')
})
xxx.fire('m-coco')
var Event = (function(){
const obj = {}
const on = (evt,handler)=>{
obj[evt] = obj[evt] || []
obj[evt].push({
handler:handler
})
};
const fire = (evt,params)=>{
if(!obj[evt]) return
for(var i=0;i<obj[evt].length;i++){
obj[evt][i].handler(params)
}
};
const off = (evt)=>{
if(obj[evt]){
delete obj[evt]
}else{
console.log('此'+evt+'无效')
}
}
return {
on: on,
fire: fire,
off: off
}
})()
Event.on('change',(val)=>{
console.log('change..now val is ' + val)
})
Event.fire('change','饥人谷')
Event.off('change')