Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Function Prototype Property > Instances That Inherit Properties from the Pro...

Instances That Inherit Properties from the Prototype Will Always Get the Latest Values

The prototype property is dynamic in the sense that instances will always get the latest value from the prototype, regardless of when it was instantiated, changed, or appended. In the code below, we create a Foo constructor, add the property x to the prototype, and then create an instance of Foo() named FooInstance. Next, we log the value of x. Then we update the prototype’s value of x and log it again to find that our instance has access to the latest value found in the prototype object.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var Foo = function Foo(){};

Foo.prototype.x = 1;

var FooInstance = new Foo();

console.log(FooInstance.x); // logs 1

Foo.prototype.x = 2;

console.log(FooInstance.x); // logs 2, the FooInstance was updated
</script></body></html>

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint