红宝书第6章.面向对象的程序设计把我当做一棵树叭

//创建Object的实例,然后添加属性和方法letperson=newObject();person.name="AAA";person.sayName=function(){};//对象字面量模式letperson={name:"AAA",sayName:function(){}};6.1.1属性类型

用特性(attribute)描述属性(property)的各种特征

两种属性:1.数据属性2.访问器属性

1.数据属性

[[configurable]]:能否delete属性后从而重定义属性,能否修改属性的特性,能否把属性修改为访问器属性。默认true。改为false后不可逆。

[[enumerable]]:能否for-in循环返回属性。默认true。

[[writable]]:能否修改属性的值。默认true。

[[value]]:属性的数据值。默认undefined。

letperson={name:’AAA’},如此直接定义时特性取默认值和数据值。

1.访问器属性

[[get]]:读取属性时调用的函数。默认undefined。

[[set]]:写入属性时调用的函数。默认undefined。

6.1.2定义属性的特性

定义单个

Object.defineProperty(obj,property,descriptor);//descriptor描述符descriptor={configurable:true,enumerable:true,writable:true,value:"example"};定义多个

Object.defineProperties(obj,{property:{configurable:true,enumerable:true,get:function(){},set:function(){}}});6.1.3读取属性的特性

Object.getOwnPropertyDescriptor(obj,property)

Object.getOwnPropertyDescriptors(obj)

6.2.1工厂模式

用函数来封装以特定接口创建对象的细节

functioncreatePerson(name){letobj={name:name,sayName:function(){alert(this.name);}};returnobj;}letperson1=createPerson("lpr");虽然解决了多个相似对象的问题,但却没有解决对象识别的问题(即怎么知道一个对象的类型)

6.2.2构造函数模式

functionPerson(name){this.name=name;this.sayName=function(){alert(this.name);};}letperson1=newPerson("lpr");letperson2=newPerson("lpd");跟工厂模式的不同:

改造函数实际经历4个步骤

这样创建的实例都有一个constructor(构造函数)属性,该属性指向Person

person1.constructor===person2.constructor===Person

person1instanceofObject//true

person1instanceofPerson//true

因此可以得出实例来自同一种类型,但是其中的方法不是同一个Function实例

解决方案:

functionPerson(name){this.name=name;this.sayName=sayName();}functionsayName(){alert(this.name);}这样做确实解决了两个函数做同一件事的问题,新问题:1.全局定义的方法直给某一个对象调用,全局作用域名不副实2.多个方法多次在全局定义函数,没有封装性

6.2.3原型模式

Person.prototype.isPrototypeOf(person1);//trueObject.getPrototypeOf(person1)===Person.prototype;//true2.原型与in操作符

person1.hasOwnProperty("name");//是否自身存在属性

alert("name"inperson1);//属性是否存在于自身或原型中

可组合判断属性存在实例还是原型中

Object.keys(obj);//获取对象上自身可枚举的属性(不含Symbol),返回结果是key的数组

Object.getOwnPropertyNames();//获取对象上自身所有(可枚举+不可枚举)的属性,如'constructor'

3.更简单的原型语法

functionPerson(){}Person.prototype={constructor:Person,//每创建一个函数会同时创建它的prototype对象,这个对象也会自动获取constructor属性。//如果不重新指定,即实例的constructor不指向Person//即person1.constructor!==Person,因此要重新指定建立联系//不过这种方式会使constructor属性的特性[[Enumerable]]为true,变为可枚举name:"AAA",sayName:function(){console.log(this.name);}};//但是上述constructor变成可枚举了Object.defineProperty(Person.prototype,"constructor",{enumerable:false,value:Person});4.原型的动态性

原型对象的问题

例如原型中有个属性是数组,在实例中push,则添加到了原型中的数组里。即变成共享。

6.2.4组合使用构造函数模式和原型模式

构造函数用于实例属性,原型模式用于方法和共享方法。

每个实例都有自己的一份实例属性,又同时共享对方法的应用。是使用最广泛、认同度最高的创建自定义类型的方法。

6.3.1原型链

最顶层的原型为Object.prototype

//都为trueinstanceinstanceofObjectinstanceinstanceofSuperTypeinstanceinstanceofSubTypeObject.prototype.isPrototypeOf(instance)SuperType.prototype.isPrototypeOf(instance)SubType.prototype.isPrototypeOf(instance)3.谨慎定义方法给原型添加方法的代码一定要放在替换原型的语句之后

通过原型链实现继承时,不能使用对象字面量创建原型方法。因为这样做会重写原型链

6.3.2借用构造函数(伪造对象或经典继承)

functionSuperType(){this.color=["red","green","blue"];}functionSubType(){SuperType.call(this);//借调了超类型的构造函数}传递参数functionSuperType(name){this.name=name;}functionSubType(){SuperType.call(this,"lpr");}

THE END
1.INPERSON的意思是()A亲自B侦查员C本人D调查in person的意思是(___)。 A.亲自 B.侦查员 C.本人 D.调查 正确答案 点击免费查看答案 试题上传试题纠错 TAGS 意思亲自亲身侦查侦察本人调查关键词试题汇总大全 本题目来自[12题库]本页地址:https://www.12tiku.com/newtiku/919848/35903035.html 来搜一搜...https://m.12tiku.com/newtiku/919848/35903035.html
2.《预订BusinessCommunication:InPerson,InPrint,Online...当当上海外文书店旗舰店在线销售正版《预订 Business Communication:In Person, In Print, Online》。最新《预订 Business Communication:In Person, In Print, Online》简介、书评、试读、价格、图片等相关信息,尽在DangDang.com,网购《预订 Business Communication:In Phttp://product.dangdang.com/590933249.html
3.theteenageyearsaresomeofthebesttimedinperson阅读理解...person阅读理解 扫码下载作业帮搜索答疑一搜即得 答案解析 查看更多优质解析 解答一 举报 The teenage years are some of the best time in each one's whole life, where they have not only a good time, but a bad time as well. Growing up physically and mentally must be responsible for all of th...https://www.zybang.com/question/aeef1da4b480389b2e0d83235ccc290f.html
1.InCreate seamless customer experiences that can help grow your business online and in person—with a single vault that scales and flexes across channels. Improve operational efficiency Reduce operating costs and stay agile with modular, interoperable, and easy-to-integrate software and hardware. ...https://www.paypal.com/us/enterprise/in-person-payments
2.PRIinPerson2023highlightsPRI in Person 2023 highlightsPRI in Person 2023 brought together nearly 1,400 investment professionals. The theme of this year’s event was ‘moving from commitments to action’. Over three days in Tokyo, across nearly 40 conference sessions and 50 side events, speakers and delegates came ...http://www.unpri.org/news-and-events/past-events/pri-in-person-2023-highlights
3.UC说fall要全inperson,谁给他们的自信啊…ucsd在读,前两天学校发邮件说fall要改成in person,我本来还不信,发邮件问了,学校回我说真的要全部,全部改成in person,不是现在这种部分in person的,就连网课选择都没有。我想想美国这疫苗接种覆盖率,再想想全面恢复以后学校食堂宿舍和教室的人口密度,真的觉得很危险。他们要恢复的话也得确定每个学生都打了疫苗...https://www.douban.com/group/topic/219946674/
4.inpersondigitalbusinesscardontheAppStoreTap your Inperson Card to the back of any smartphone or use the QR code on the card to instantly share all your information. SHARE ONLINE Share your digital business card online by adding it to social media, your email signature or sending it via chat. ELIMINATE MANUAL WORK AND SHORTEN FO...https://apps.apple.com/md/app/inperson-digital-business-card/id1576673890
5."inperson"是什么意思"inperson"的中文翻译in-person 简明英语-中文词典 < in-person ><<形容词>>亲自的; 在场的, 现场的 与"in-person"相近的词条 ... in-patient tv monitor in-peak indication in pediatric field in pelvis in perpetuum in-person in phase、in-phase in place in-place computation in place of in plane in-plant in plen...http://www.mcd8.com/w/in-person
6.VermontInPersonVermontInPerson.com Presents Under The Golden Dome 2020 Week 9 Ninth week of the 2020 Vermont legislative session March 11, 2020. State House Info – Vermont Art Teachers Association Exhibit –“Earth, Student Images Celebrating Our Stewardship of the Natural World for Youth Art Month.” Energy ...http://www.vermontinperson.com/
7.InPersonLakeyahOGPARKERDJDramaTyga高音质在线试听Oh you gotta see this in person OG Parker B***h I look better in person person Can't f**k with me like a virgin virgin I don't got opps I got options options Coupe with a groupie She topless Talkin' 'bout me I'm the topic aye ...https://www.kugou.com/mixsong/5lz86c0c.html
8.People’sRepublicofChinain:IMFStaffCountryReports...Hong Kong Special Administrative Region, People's Republic of China India Indonesia Japan Kiribati Korea, Democratic People's Republic of Korea, Republic of Lao People's Democratic Republic Macao Special Administrative Region, People's Republic of China Malaysia Maldives Marshall ...https://www.elibrary.imf.org/view/IMF002/12733-9781475502985/12733-9781475502985/12733-9781475502985_A001.xml
9.EDAHostsFirstinPersonConferenceinParkCity,UTSeattle, WA -- June 14, 2021 – Elite Distributor Alliance hosted the industry’s first face to face meeting in Park City Utah at the Grand Summit resort. All of EDA’smembers and vendors attendedthe event and everyone was excited to be meeting in person. “After a year and a h...http://www.me-mag.com/mobile-electronics-industry-news/item/46988-eda-hosts-first-in-person-conference-in-park-city,-ut
10.adpersonin是什么意思adpersonin的中文意思adpersonin 名词 从事广告业的人相关短语 N. N. N. N. N. N. culture medium 【医】 N.N.N.培养基(含琼脂、盐、兔血,用以培养黑热病病原体) N. P. N. 【医】 非蛋白氮 chen n. yang n. 杨振宁 R. N. 1. registered nurse 注册护士 2. Royal Navy N. clevelandii 克利夫兰烟【植】 regu...http://m.hujiang.com/ciku/w_adpersonin/
11.大英四考试5.你最好亲自去,而不要派某人代表你。(in person) Instead of sending somebody on your behalf,you had better go in person. inperson:亲自 instead of:代替;而不是… behalf:n. 代表;利益 6.他预测在未来的几年里这个地区不会发生地震。(break out) ...https://blog.csdn.net/weixin_34236497/article/details/92376220
12.InpersonassistanceforenrollingintheDigitalCertificate...For those who receive a cancelation notice after registration, please disregard the notice as it may be a system glitch. All registration will be honored and accepted and everyone who registers will be assisted during the online or in person session ...https://www.un.org/other/afics/events/person-assistance-enrolling-digital-certificate-entitlement
13.TravelHouseUKIn cases, where no email address is provided, the traveller may visit the office in person to collect tickets or request tickets in post (mailing/post may take less/more than 72 working hours). The confirmatory email sent after telephonic / online reservation and payment is "not a proposal ...https://www.travelhouseuk.co.uk/booking_conditions.htm
14.Object类型有那些?,object类有什么特点letperson={name:"Alice",age:25,city:"Paris"}; for(letkeyinperson){ console.log(key+":"+person[key]); } Object.keys():返回对象自身的可枚举属性的键数组。 letkeys=Object.keys(person); console.log(keys);//输出:["name","age","city"] ...https://www.litecc.com/news/chanpingxunxi/1589.html
15.ES6—class类详细教程(下)腾讯云开发者社区classPerson{#name='Dapan';// ...staticisNameInPersonClass(obj){if(#nameinobj){return'这个对象是Person类的实例';}else{return'这个对象不是Person类的实例';}}}constperson4=newPerson();constperson5=Object.create(null);console.log(Person.isNameInPersonClass(person4));// 这个对象是Person类的...https://cloud.tencent.com/developer/article/2122388