javascript如何创建对象
JS是基于对象的语言,可以使用面向对象思想模拟JAVA|C++之类的面向对象语言。
•面向过程
◦关注解决问题的步骤
•面向对象
◦关注的是解决问题的所需要的对象(内容和角色),然后根据业务逻辑按一定规则调用相关方法
对象分为系统对象和自定义对象两种。我们可以通过调用系统构造函数来创建出系统对象,如:array|date等。自定义对象必须自己创造,无法利用系统函数来创造。
javascript创建对象
一、直接创建
//直接创建
//JS创建对象
//1:创建空对象
varperson1=newObject();
//2:将该对象所需要的属性、方法加进去
person1.name="ailer";
console.log(person1.name);
person1.gender="male";
//3:该对象添加的方法|(函数)
person1.manager=function(){
console.log("AilerismyEnglishname");
}
//4:调用对象方法:对象.方法名();
person1.manager();
//函数|方法?函数属于对象时,该函数属于这个对象下的方法;通过方法名来调用该函数;
//变量|属性?当变量属于某一个对象时候,该变量就是这个对象下的方法。通过属性名来调用变量。
//增
person1.age="6";
//改
person1.name="lemon";
//查
console.log(person1.name);
//删
deleteperson1.age;
console.log(person1.age);==>undefined
//引用类型,存储的是地址
//基本类型:存储的是值标志位
/*vararr1=[1,2,3,4]
vararr2=[5,6,7,9];
vararr2=arr1;//
arr2[0]=10;//更改arr2里面的值,arr1也更改
alert(arr1[0]);//====>10引用类型*/
varper2=newObject();
per2.name="Relia";
per2.age="18";
per2.gender="femal";
per2.hobby="lemons";
//1:通过.(点语法)访问属性
//2:通过[](方括号)访问对象的属性;方括号中必须是属性字符串或保存属性字符串的变量|遍历属性的时候才使用方括号
varn="name"
//console.log(per2["name"]);//双引号
console.log(per2[n]);
for(varpropertyinper2){
//console.log(per2[property]);
}
虽然直观,但是创建多个对象的时候,代码冗余
二、使用函数创建(工厂模式)
为了解决多个类似对象声明的问题,我们可以使用一种叫做工厂模式的方法,这种方法就是为了解决实例化对象产生大量重复的问题。
//定义构造函数
functioncreatePerson(name,age){
//创建一个新的空对象
varperson=newObject;
//添加属性|方法
person.name=name;
person.age=age;
person.manager=function(){
console.log("ai");
}
//返回
returnperson;
}
varper1=createPerson("ailer",12);
console.log(per1.name);
varper2=createPerson("lemon",23);
console.log(per2.age);
console.log(per2instanceofObject);//true
console.log(per2instanceofcreatePerson);//false//无法区分该对象类型
console.log(per2.manager==per1.manager);//false可得出per1和per2各自开闭空间
优:批量创建同类实例
缺:实例用同类属性,造成内存浪费无法公,且无法区分该对象的类型
三、字面量创建
优:简单直接
缺:无法批量构建同类对象
//字面量创建的对象使用constructor属性不会指向实例,而是指向object
//使用字面量创建
varper1={
name:"Ailer",
constructor:per1,
age:12,
gender:"female",
hobby:"play",
eat:function(){
console.log(this.name);
}
}
per1.eat();//ailer
per1.name="lemon";
per1.eat();//lemon
console.log(typeofper1);//Object
console.log(per1.constructor==Object);//true
四、new+构造函数
//构造函数创建对象,其子对象用instanceof不识别,所有采用new+obj创建
//对象识别了,但是仍然浪费一些代码区;==>产生原型创建
//创建js对象new+构造函数
//1:创建构造函数|通常首字母大写
functionCreatePerson(name,age){
//2:把对象的属性|方法挂靠在this指针身上,当调用该函数创建对象时,this指针就指向这个新对象;
//这个this就添加给这个对象
this.name=name;
this.age=age;
/*this.speak=function(){
//此处this也指向创建对象
console.log(this.name+"hello");
}
}
/*CreatePerson.prototype.gender="20";
CreatePerson.prototype.ea=function(){
console.log(this.name+"sfd");
}*/
//__proto__:是:实例对象中的原型属性,指向对应构造函数对应的原型对象
//[[prototype]]
//调用
varper1=newCreatePerson("ailer","20");
varper2=newCreatePerson("relia","18");
console.log(per1instanceofCreatePerson);//==true
console.log(per2instanceofCreatePerson);//==>true
console.log(per1.speak==per2.speak);//==false说明系统开辟了两个不同的代码区,造成了内存浪费.
字面量创建一个比较方便,所以产生构造函数,普通构造函数(工厂模式),子对象instanceof不识别且内存浪费,用new+构造函数,子对象识别了,但仍有部分代码重复,内存浪费,产生原型代码解决。
五、原型模式
functionCreateAnimal(name,age){
//1.2:把外部参数绑定实例属性
this.name=name;
this.age=age;
}
//1.3把相同的属性,绑定在原型上(原型属性,原型方法)
CreateAnimal.prototype.gender="male";
CreateAnimal.prototype.style=function(){
console.log(this.name+"ailers");
};
//2:调用构造函数创建对象
varcat1=newCreateAnimal("xiaohua","2");
varcat2=newCreateAnimal("xiaohua","2");
cat1.style();
console.log(cat1.style==cat2.style);//方法引用地址一样,将属性放到原型对象中,节约地址
//instanceof可以来判断对象属于哪一个【函数】
//constructor建造者也可以用来判断对象属于哪个【构造函数】【常】
//实例对象保存一个constructor属性指向它的构造函数
//instanceofandconstructor区别
console.log(cat1instanceofCreateAnimal);//true
console.log(cat1instanceofObject);//true
console.log(cat1.constructor==CreateAnimal);//true
console.log(cat1.constructor==Object);//==false
//构造函数的原型也有constructor属性指会构造函数
console.log(CreateAnimal.prototype.constructor==CreateAnimal);//true
//in判断该属性是否存在这个对象中,这个属性为实例属性或原型
//alert("name"incat1)//true
//alert("gender"incat1);//true
//hasOwnProperty:来判断某一个属性到底是实例属性,还是继承自原型属性if是为true,else不存在|不是返回false;
console.log(cat1.hasOwnProperty("aaa"));//false不存在的属性返回为false
console.log(cat1.hasOwnProperty("name"));//true实例属性
console.log(cat1.hasOwnProperty("style"));//false原型属性返回为false
//遍历属性找原型属性
//判断参数是否为原型属性工具类
console.log(isPrototype("gender",cat1));//true
functionisPrototype(proString,obj){
if(proStringinobj){
if(!obj.hasOwnProperty(proString)){
returntrue;
}else{
returnfalse;
}
}else{
returnfalse;
}
}
/*
functionisProperty(object,property){//判断原型中是否存在属性
return!object.hasOwnProperty(property)&&(propertyinobject);
}*/
动态原型模式
//构造函数中初始化原型
functionper(name,age,gender){
this.name=name;
this.age=age;
this.gender=gender;
//只在初始化原型的时候执行一次
if(typeofthis.sayName!="function"){
Person.prototype.sayName=function(){
alert(this.name);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。