const obj = {}; console.log(obj); // [object Object] obj的toString()方法从Object的原型中查找到并使用 function Person(firstName, lastName, age) { this.firstName = firstName; this.lastName = lastName; this.age = age; // 在Person.prototype上定义了toString覆写了Object.prototype上的toString Person.prototype.toString = function() { return `${this.firstName} It Is`; } } let bob = new Person("Bob", "Ross", 21); let alex = new Person("Alex", "Wang", 21); console.log(bob); // Bob It Is console.log(alex); // Alex It Is
详解JavaScript原型与原型链(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/02677ceed60f2201d000f448743522f3.html