目录
前言
即将而立之年,父母催促,个人身体状况也大不如前,遂借此New个对象,
请看下文!
一、我的对象标准
我需要查看对象的感情经历 ,家庭状况,以及个人能力和是否长辈扶持
请看目录!
二、我查看我对象的家底和个人状况
1.查看我的对象【增删改】
对象创建【填表】:
//口头问答
var friend={};
friend.name='娜娜';
friend.bodys='膀大腰粗';
friend.face='婴儿肥';
friend.getmoney=1000;
friend.family=4;
friend.company=2;
friend.Boyfrind=0;//0代表没有 1代表有男朋友
//表格填写
var nextfriend={
name:'露露',
bodys:'S型',
face:'圆脸',
getmoney:8000,
family:4,
company:1,
Boyfrind:0
};
我查看我对象的个人属性以及修改删除
//查看家庭人数
console.log(friend.family + '' + nextfriend.family);
//身材修改--个人看到的感受
friend.bodys = '梨型';
console.log(friend.bodys);
//身材加分 不看脸 删除脸属性
delete friend.face;//删除脸这个属性
console.log(friend.face) //对象里没有属性访问会报underfind不会报错
2.提高我甄别对象的速度和她们个人自我评价
创建构造函数放入公有属性:
//构造函数 放入公有属性
function Changefrind(name,bodys,face,getmoney,family,company,Boyfrind){
this.name=name;//this代表这个实例 相当于friend2.name='Lucy'
this.bodys=bodys;
this.face=face;
this.getmoney=getmoney;
this.family=family;
this.company=company;
this.Boyfrind=Boyfrind;
}
//只要new了相当于执行下面语句 并且return隐式返回
// var this={};
// this.name=name;
// this.bodys=bodys;
// this.face=face;
// this.getmoney=getmoney;
// this.family=family;
// this.company=company;
// this.Boyfrind=Boyfrind;
// return this;
var friend2=new Changefrind('Lucy','虎背熊腰','国字',1500,4,0,0);
我的长辈发话了 需要她们来个自我评价【Changefrind.prototype 对象原型】
Changefrind.prototype.talk = function() {//Changefrind祖先
return '请来个自我评价';
}
console.log(Changefrind.prototype);//内容是原型上的属性 和构造器
我的对象开始自我评价 继承原型【祖先】里面属性
friend2.talk = function() {
return '我尊老爱幼,为人圆滑,据理力争 始终站在正确的一面';
}
var friend3 = new Changefrind('Lucy1', '虎背熊腰1', '国字1', 1500, 4, 0, 0);
console.log(friend2.talk()); // 输出我尊老爱幼,为人圆滑,据理力争 始终站在正确的一面
console.log(friend3)//里面没有talk属性
console.log(friend3.talk()); //输出请来个自我评价【继承】
解释: 当访问一个对象的某个属性时,会先在这个对象本身属性上查找,如果没有找到,则会去它的__proto__隐式原型上查找,即它的构造函数的prototype,如果还没有找到就会再在构造函数的prototype的__proto__中查找,这样一层一层向上查找就会形成一个链式结构,我们称为原型链。没有找到就是underfind 这就是原型链
原文链接:https://blog.csdn.net/weixin_44369568/article/details/90632186
3.她们向我家里提出要求 有车有房【原型增删改】
原型增删改只可以在原型上:
friend2.prototype = {
car: 'BWM'
}; //没有权限给我长辈提要求 只能告诉我去反映
Changefrind.prototype.car = 'BMW';
Changefrind.prototype.house = "3层";
console.log(friend2.car + '房子几层:' + friend2.house);
//看人长的不错 可以不用房
delete Changefrind.prototype.house; //原型删除
console.log(friend2.house);
3.我通过对象爸爸妈妈的公司创办了自己的公司
apply和call方法使用 :
function Friend2father(factorys, busstation) {
this.factorys = factorys;
this.busstation = busstation;
}
function Friend2MOther(clother, store) {
this.clother = clother;
this.store = store;
}
//我使用我对象爸爸和妈妈的工厂和汽车站进行加工
function My(name, age) {
this.name = name;
this.age = age;
Friend2father.call(this, 'factorys', 'busstation'); //call 和apply更改this指向 调用其他对象方法给自己用,区别是传参不一样【call是单个,apply是数组】
Friend2MOther.apply(this, arguments); //[clother,store] 可以改变为arguments
}
//
var myfirst = new My('Tom', 18);
console.log(myfirst);//我有她们的属性和方法
解析:call()和apply(),您能够使用属于另一个对象的方法。更改this的指向
区别是 参数不一样
var Num = {
tell: function(tvname) {
return tvname+ this.name + ':' + this.beautiful
}
}
var Num1 = {
name: '关云长',
beautiful: '单刀赴会'
}
var x = Num.tell.call(Num1,'三国');
console.log(x)
总结
1.每个函数都会有__proto__隐式属性,引用类型对象__proto__隐式属性指向原型
2.每个函数都会有prototype原型方法,他返回的是个人属性加构造器,没有构造器会系统默认创建一个