Skip to content

Commit 9c4464c

Browse files
committed
exec: explain-how-prototypal-inheritance-works
1 parent 4235698 commit 9c4464c

File tree

1 file changed

+4
-4
lines changed
  • questions/explain-how-prototypal-inheritance-works

1 file changed

+4
-4
lines changed

questions/explain-how-prototypal-inheritance-works/en-US.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This behavior simulates classical inheritance, but it is really more of [delegat
1212

1313
Here's an example of prototypal inheritance:
1414

15-
```js
15+
```js live
1616
// Parent object constructor.
1717
function Animal(name) {
1818
this.name = name;
@@ -60,7 +60,7 @@ Prototypical inheritance is a feature in JavaScript used to create objects that
6060

6161
1. **Prototypes** : Every object in Javascript has a prototype, which is another object. When you create an object using an object literal or a constructor function, the new object is linked to the prototype of its constructor function or the `Object.prototype` if no prototype is specified. This is commonly referenced using `__proto__` or `[[Prototype]]`. You can also get the prototype by using inbuilt method `Object.getPrototypeOf()` and you can set the prototype of an object via `Object.setPrototypeOf()`.
6262

63-
```js
63+
```js live
6464
// Define a constructor function
6565
function Person(name, age) {
6666
this.name = name;
@@ -104,7 +104,7 @@ console.log(john.sayHello); // undefined
104104

105105
3. **Constructor functions**: JavaScript provides constructor functions to create objects. When a function is used as a constructor with the new keyword, the new object's prototype (`[[Prototype]]`) is set to the constructor's prototype property.
106106

107-
```js
107+
```js live
108108
// Define a constructor function
109109
function Animal(name) {
110110
this.name = name;
@@ -142,7 +142,7 @@ console.log(fido.fly); // undefined
142142

143143
4. **`Object.create()`**: This method creates a new object with the specified prototype object and properties. It's a straightforward way to set up prototypical inheritance. If you create a object via `Object.create(null)` it will not inherit any properties from `Object.prototype`. This means the object will not have any built-in properties or methods like `toString()`, `hasOwnProperty()`,
144144

145-
```js
145+
```js live
146146
// Define a prototype object
147147
let proto = {
148148
greet: function () {

0 commit comments

Comments
 (0)