63. JavaScript Object Prototypes
how do we do that?
there are 2 ways
How to create many from that one?
var myFather = new person("John", "Doe", 50, "blue"); var myMother = new person("Sally", "Rally", 48, "green");
Can you modify default values in the prototype with prototype value?
yes but Only modify your own prototypes. Never modify the prototypes of standard JavaScript objects.
What kind of values do we consider the values of prototype properties?
(default values).
Adding Methods to a Prototype
Adding Methods to a Prototype
Adding Properties and Methods to Objects
Adding Properties and Methods to Objects
Adding Properties to a Prototype
Adding Properties to a Prototype
Adding a Method to an Object
Adding a Method to an Object
Adding a Property to an Object
Adding a Property to an Object
Where do all js objects inherit their properties and methods?
All JavaScript objects inherit their properties and methods from their prototype.
Creating a Prototype
Creating a Prototype
What does every object have in js?
Every JavaScript object has a prototype.
JavaScript Prototypes
JavaScript Prototypes
What kind of properties do we define in the constructor function?
Prototype properties
In what cases do we want to add properties or methods to an object?
Sometimes you want to add new properties (or methods) to an existing object. Sometimes you want to add new properties (or methods) to all existing objects of a given type. Sometimes you want to add new properties (or methods) to an object prototype.
What does prototype property allow us to do?
The JavaScript prototype property allows you to add new properties to an existing prototype:The JavaScript prototype property also allows you to add new methods to an existing prototype:
What do we call a prototype?
The constructor function is the prototype for your person objects. function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }
To which object will "English" be added?
The property will be added to myFather. Not to myMother. Not to any other person objects.
What type of container is the prototype?
The prototype is also an object.
How do we create a prototype?
The standard way to create an object prototype is to use an object constructor function:
what is way 1?
To add a new property to a constructor, you must add it to the constructor function:
Using the prototype Property
Using the prototype Property
What is the advantage of using a constructor function?
With a constructor function, you can use the new keyword to create new objects from the same prototype:
do we add properties to a prototype the same way as you add a new property to an existing object?
You cannot add a new property to a prototype the same way as you add a new property to an existing object, because the prototype is not an existing object.
how to define a method in the constructor function?
function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.name = function() {return this.firstName + " " + this.lastName;}; }
example of that
function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; this.nationality = "English" }
example of that
function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; }
example of adding new method to an existing prototype
function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; } person.prototype.name = function() { return this.firstName + " " + this.lastName; };
example of adding new properties an existing prototype
function person(first, last, age, eyecolor) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eyecolor; } person.prototype.nationality = "English";
What is an object prototype?
it is a constructor function which can be used as base for as many objects as we want.
What does inherit mean?
it means onnan veszik elő, ott tárolják. it means that for every object in js the properties and methods are declared in a prototype (object cosntructor function)
Why do we all prototype?
maybe because we create one and from that one we can create many.
Adding a new method to an existing object is also easy:
myFather.name = function () { return this.firstName + " " + this.lastName; };
Adding a new property to an existing object is easy:
myFather.nationality = "English";