prototype 为何是不能被打印显示的呢? 那么它的意义是什么? function Person(name, age) { this.name = name; this.age = age; } Person.prototype.sayHello = function() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); } let person1 = new Person('John', 30); person1.sayHello(); // 可调用 console.log(person1); // 没有原型上被定义的数据 console.log(person1.prototype); // undefined
原型和原型对象到底指的是什么? 看到一些描述里,又有原型一词,又有原型对象一词(可以查看我的另一个"问题" (https://segmentfault.com/q/1010000044547471?_ea=335044064),就同时遇到了这两个词)。 * 这两者分别是指谁?还是一样的意思呢? * 是函数里面经常提到的"prototype"还是在原型链中经常提到的"[[Prototype]]"? * 还是没有明确的规定只是个人的随意起了名字? 今天在看babel的"文档" (https://link.segmentfault.com/?enc=8kDRnfO4l%2BRb6qNQtcNurQ%3D%3D.7%2BrtUeOlk%2F%2FxEA0raYtP5%2FsPWsZPOTNXKT1WShI9StgIDTUeXLzfA0gtoG41Yaus)时又遇到了"原型对象"一词,根据上下文看,这里的"原型对象"说的应该是"prototype"而不是"[[Prototype]]" "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241015/46af764fbbab1f6c5c9ede710ade30fa.png) 顺便说一下这里的翻译: * "内置对象"改为"内置函数"更合适一吧。当然函数也是个对象。 * "添加对象"应该是"添加属性"吧 附"英文原文" (https://link.segmentfault.com/?enc=KuqMDPAVEqwXVGtOxTGVDQ%3D%3D.%2BbxIz95tCNMv5mkjEUG9cbzoD576IIcpmrWjiiPY%2BYw%3D) "image.png" (https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241015/3fe2d0c239733260c6fe553208c2a606.png)