0

I now in javascript. I have a qustion about inhertants by prototype. How can takes a year with out use proto I have function Vehicle

 function Vehicle(name,color,year){
            this.name=name;
            this.color=color;
            this.year=year;
        };

and function liveTime

Vehicle.prototype.liveTime=function(){
            return new Date().getFullYear() -this.year;
            
        };

we can inhertans by prototybe (BUT prototype is a property of a Function object. It is the prototype of objects constructed by that function.)

here we can inhertance by it

 function Vehicle(name,color,year){
            this.name=name;
            this.color=color;
            this.year=year;
        };


        Vehicle.prototype.liveTime=function(){
            return new Date().getFullYear() -this.year;
            
        };

        var car= new Vehicle('y','red',2000);
        console.log(car.liveTime());

The Qustion is why can inhertans by prototype without use proto

Shaimaa
  • 13
  • 2
  • e.g. [Introducing JavaScript objects :: Object prototypes](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes) – Peter Seliger Aug 19 '22 at 09:54
  • Does this answer your question? [How does JavaScript .prototype work?](https://stackoverflow.com/questions/572897/how-does-javascript-prototype-work) – Peter Seliger Aug 19 '22 at 09:55
  • I read it but I don't understand – Shaimaa Aug 19 '22 at 10:00
  • The OP also could give a try to ... Dmitry Soshnikov's [JavaScript. The Core: 2nd Edition - Prototype](http://dmitrysoshnikov.com/ecmascript/javascript-the-core-2nd-edition/#prototype) ... by playing around with the various mentioned techniques of accessing (and altering) an object's prototype. – Peter Seliger Aug 19 '22 at 10:54
  • "*…why can inhertans by prototype…*" because that's how the language is specified to work. Constructors (functions) have a public *prototype* property, instances created by a constructor inherit via their private `[[Prototype]]` property that references the constructor's *prototype*. An object's properties are resolved first on the object itself, then on the private `[[Prototype]]` chain. – RobG Aug 19 '22 at 10:57

0 Answers0