![]() catConstructor on the other hand, is given a prototype of animalConstructor. The Cats function returns an object with catConstructor as it's prototype. ![]() tPrototypeOf is a method which takes in two arguments - the object (first argument) and the desired prototype (second argument).įrom the above, the Animals function returns an object with the animalConstructor as prototype. tPrototypeOf(catConstructor, animalConstructor) Ĭonst clara = Cats("Clara", 33, "purple") ![]() The following methods are used to achieve this: Object.create() function DogObject(name, age) whiskers` This property can be modified by explicitly stating that it should refer to another prototype. This is the property on every object that gives it access to the Object prototype property.Įvery object has this property by default, which refers to the Object Protoype except when configured otherwise (that is, when the object's _proto_ is pointed to another prototype). This points to the object which is used as a prototype. They do not possess these properties, but are granted access to the properties in the prototype. In other words, all objects have access to the Object's prototype. If you look closely, you'll also notice our hidden hasOwnProperty. All these properties are coming from the global Object prototype. You'll see a set of properties under the Object constructor. Notice the last property - _proto_? Try expanding it: The _proto_ property of names When you expand the results in the console, you'll get this: console.log(names) Try logging the contents of the variable to the console: console.log(names) Well, it comes from the Object prototype. The object variable names has only two properties - fname and lname. The most popular model of OOP is class-based.īut as I mentioned, JavaScript isn't a classed-based langauge – it's is a prototype-based langauge.Ī prototype-based language has the notion of a prototypical object, an object used as a template from which to get the initial properties for a new object.Ĭonsole.log(names.hasOwnProperty("mname")) In this tutorial, I'll explain OOP and show you how to use it.Īccording to Wikipedia, class-based programming isĪ style of Object-oriented programming (OOP) in which inheritance occurs via defining classes of objects, instead of inheritance occurring via the objects alone But it still has ways of using object oriented programming (OOP). JavaScript is not a class-based object-oriented language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |