OOP with Javascript? | Sololearn: Learn to code for FREE!

+3

OOP with Javascript?

5/5/2018 9:24:03 PM

Cristopher Pereira Moreira

11 Answers

New Answer

+10

standard: function Animal(name, age, type){ this.name = name; this.age = age; this.type = type; } Animal.prototype.getName(){ return this.name; } var dog = new Animal("Snoop", 420, "Dogg"); var pengu = new Animal("Pingu", 69, "Penguin"); //////////////// or by object var Animal = { name: "", age: 0, type: "", getName: function(){ return this.name; }, } var dog = Object.create(Animal); dog.name = "Snoop"; dog.age = 420; dog.type = "Dogg": ///////////// Now theres a class identifier but idk how to use it properly. I prefer oldschool ways

+9

KrOW wow JS class is seriously that easy? Articles complicate it so much xD

+8

KrOW thanks for sharing :)

+7

+4

... and last ES version class form: class Animal{ constructor(name,age,type){ this.name= name this.age= age this.type= type } getName(){ return this.name } }

+4

「HAPPY TO HELP」 While there are some pitfalls, basically its simple ... I have used some of it in my code https://code.sololearn.com/WmDleRRFZ2W7/?ref=app

+3

Really looking forward to browsers in general allowing ES6 & 7 without having to transpile back to ES5. Prototype is OOP but will be nice to have the same language syntax as every other language.

+3

👍👍

+3

we have few helping posts regarding that in here somewhere but there's a good book as well packt object oriented JavaScript , might find download links on Google for free https://www.sololearn.com/discuss/739193/?ref=app

+1

thanks standard way reflects better for me the object creation

+1

there are cool things with TypeScript and ReactJS thanks guys!