About Object and Extensions

Asked

Viewed 28 times

1

I am now starting in Javascript I am learning through websites. I went to an excellent site (http://tableless.github.io/iniciantes/manual/js/objetos.html) and so far everything was fine. Except that getting to the part of (Objects) the code does not turn out to be anything. The point is that the code does not seem wrong to me. I don’t think I’m putting the code in full or I’m not saving it in the right extension. I’ve thought about a lot of things and since it doesn’t talk much about inserting in the code in HTML (which is what I’ve been doing), maybe it’s to use a program of its own or something. I ask you to help me.

  • 1

    Hello, welcome. Are you a beginner in javascript and html? What part of the code are you trying to implement? Could you show?

  • Everyone bumps into this question of Javascript object. And that actually the concept of object orientation in the Javascript language is different than other languages. There where a lot of material on the net wants to get into this class approach that is specifically paradigm (Object Oriented), and Javascript has no native class. Everything in the language is an object (Array, Functions, Number...) which consequently has its object constructor, where you encapsulate its properties/methods. I would advise to understand the language more deeply and then apply object-oriented programming!

1 answer

0

Well, I will consider that you are running this code below:

var album = new Object();
album.title = "Metallica (Black Album)";
album.released = 1991;
album.showInfo = function() {
  alert("Título do álbum: " + this.title + "Lançado em: " + this.released);
};
album.showInfo();

Run and see in Alert the result.

This is a didactic example, explained step by step by the author. At a certain point he says:

Since methods are functions, you should add a pair of parentheses - () - when accessing them.

So you just call the method (which is a function) of your object:

album.showinfo();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.