JS的面向对象与原型
const yoshi = { skulk: true }; const hattori = { sneak: true }; const kuma = { creep: true }; ⇽--- 创建3个带有属性的对象 assert("skulk" in yoshi, "Yoshi can skulk"); assert(!("sneak" in yoshi)), "Yoshi cannot sneak"); assert(!("creep" in yoshi)), "Yoshi cannot creep"); ⇽--- yoshi对象只能访问自身的属性skulk Object.setPrototypeOf(yoshi, hattori); ⇽--- Object. setProto-typeOf方法, 将对象hattori设置为yoshi对象的原型 assert("sneak" in yoshi, "Yoshi can now sneak"); ⇽--- 通过将hattori对象设置为yoshi对象的原型, 现在yoshi可以访问hattori对象的属性 assert(!("creep" in hattori)), "Hattori cannot creep"); ⇽--- 目前hattori对象还不具有属性creep Object.setPrototypeOf(hattori