JS对象封装常用方式1.常规封装函数Person(name,age){this.name=name;这个。年龄=年龄;}Pserson.prototype={constructor:Person,say:function(){console.log('大家好!');}}2。升级版(更常用)functionPerson(example){this._init_(example);}Pserson.prototype={constructor:Person,_init_:function(example){this.name=example.name;this.age=example.age;}say:function(){console.log('大家好');}}3。new的执行原理varmyNew=function(constructor,args){varobj={};obj.__proto__=constructor.prototype;varres=constructor.apply(obj,args);vartype=typeofres;if(['string','number','boolean','null','undefined'].indexOf(type)!==-1){returnobj;}returnres;}解释:通过varobj={}构造一个空对象。构造函数将原型属性prototype赋值给obj的原型对象__proto__,并执行this.in它(例子);这句话,对象obj可以在其原型对象中查找_init_方法(原型链)。varres=constructor.apply(obj,args);使用obj作为上下文调用函数,同时将参数作为数组传递。然后,this._init_(example);将由obj调用以执行函数_init_:function(example){this.name=example.name;this.age=example.age;}以obj作为上下文,o也将具有自己的名称、年龄属性。如果在构造函数中,返回复合类型,包括对象、函数、正则表达式,则直接返回对象,否则,objvartype=typeofres;if(['string','number','boolean','null','undefined'].indexOf(type)!==-1){returnobj;}returnres;示例函数Person(name){this.name=name;}Person.prototype.say=function(){console.log(this.name);}varjack=myFriend(Person,['jack']);控制台日志(杰克);杰克说();4.jQuery类封装了jQuery对象,具有很多强集成,可以作为函数调用,也可以作为对象调用,作为函数调用时,可以返回一个实例,无需new。代码varPerson=function(info){returnnewPerson.prototype.init(info);}Person.prototype={constructor:Person,init:function(){this.name=example.name.}}人物原型。init.prototype=Person.prototype;这种封装方式非常巧妙。把对象的构造操作放在函数里面,自己充当工厂。一直调用原型是不直观的,所以varPerson=function(example){returnnewPerson.fn.init(example);}Person.fn=Person.prototype={constructor:Person,init:function(){this.name=info.name;this.say=function(){this.makeExp();}}makeExp:function(){console.log(this.name);}}//虽然makeArray等常用方法被链接到了Person.prorotype下,但是init实例还是会用到它。Person.fn.init.prototype=Person.fn;最后用闭包封装varPerson=(function(win){varPerson=function(name){returnnewPerson.fn.init(name);}Person.fn=Person.prototype={constructor:Person,init:function(name){this.name=name;this.say=function(){this.makeExp();}},makeExp:function(){console.log(this.name);}}Person.fn.init.prototype=Person.fn;返回人;})()示例:varpeople=Person('jack');console.log(people);people.say();object.create();一种构造对象的方式,可以传递一个对象Person,构造一个人,让人继承Person.varPerson={name:'jack',say:function(){console.log(this.name);}}varpeople=Object.create(Person);console.log(people);people.say();对象Person的属性变成了人的原型属性,也就是说人的原型继承自Person!我们可以实现一个Object.create()Object.create=function(prototype){functionFun(){};Fun.prototype=原型;varobj=new乐趣();returnobj;}说明:使用Person作为构造函数的原型属性,即可构造以Person为原型对象的Objects.Object.create(prototype);创建继承原型的实例对象该方法的一些注意事项:(1)如果参数为Object.prototype,则创建的原型为Object.prototype与newObject()创建的对象相同Object.create(Object.原型)<==>新对象();(2)如果传入的参数为空或null,则创建的对象没有原型如果对象不能用document.write()打印出来,会报错,因为document.write()打印的原理是调用Object.prototype.toString()方法,对象没有原型,所以没有这个方法,所以文档.write()不能打印由此扩展的知识点:引用值也算作对象,所以可以用document.write()打印;原始值numebr、boolean、string都有自己的对象包装类,借助这种机制也可以用document.write()打印出来;但是undefined和null既不是引用值也不是对应的包装类,所以不应该打印出来,但是你会发现这两个值也可以用document.write()打印出来,因为这两个值被设置为特殊值,document.write()不调用任何方法打印它,而是直接打印它的值
