Explore the difference and usage between prototype and prototype chain
In JavaScript, object-oriented programming is a commonly used programming method. Prototype and prototype chain are two important concepts when doing object-oriented programming. This article explores the differences between prototypes and prototype chains and how to use them, providing concrete code examples.
Basic concepts of prototype and prototype chain:
__proto__
attribute. __proto__
attribute. When accessing a property of an object, if the object itself does not have the property, it will be looked up along the prototype chain. The difference between prototype and prototype chain:
Object.getPrototypeOf(obj)
. Methods using prototypes and prototype chains:
Creating constructors and instance objects:
function Person(name) { this.name = name; } Person.prototype.sayHello = function() { console.log('Hello, ' + this.name); }; var person1 = new Person('Alice'); person1.sayHello(); // 输出:Hello, Alice
Inherit properties and methods:
function Student(name, grade) { Person.call(this, name); // 调用父类构造函数 this.grade = grade; } Student.prototype = Object.create(Person.prototype); // 继承父类原型 Student.prototype.constructor = Student; // 修复构造函数 Student.prototype.study = function() { console.log(this.name + ' is studying in grade ' + this.grade); }; var student1 = new Student('Bob', 5); student1.sayHello(); // 输出:Hello, Bob student1.study(); // 输出:Bob is studying in grade 5
Find properties and methods:
console.log(student1.name); // 输出:Bob console.log(student1.__proto__ === Student.prototype); // 输出:true console.log(student1.__proto__.__proto__ === Person.prototype); // 输出:true console.log(student1.__proto__.__proto__.__proto__ === Object.prototype); // 输出:true console.log(student1.hasOwnProperty('name')); // 输出:true console.log(student1.hasOwnProperty('sayHello')); // 输出:false
Through the above code With the example, we can clearly understand the role and use of prototypes and prototype chains. The prototype provides the ability for objects to inherit properties and methods, and the prototype chain realizes the sharing of properties and methods between objects. Using prototypes and prototype chains can improve code reusability while reducing memory consumption. However, in actual development, it should be noted that too long a prototype chain may cause performance problems, so the inheritance relationship of objects needs to be designed reasonably.
Summary:
In JavaScript, prototypes and prototype chains are important concepts in object-oriented programming. The prototype provides the ability to inherit properties and methods, and the prototype chain realizes the sharing of properties and methods between objects. By properly using prototypes and prototype chains, code reusability and performance can be improved. I hope this article will be helpful to the understanding and use of prototypes and prototype chains.
The above is the detailed content of Analyze the similarities, differences and application methods of prototypes and prototype chains. For more information, please follow other related articles on the PHP Chinese website!