Object orientation with jQuery/Javascript

Asked

Viewed 650 times

1

I’ve read several things about it,:

   var classe = function () {
        var metodo = function () {
            return 0;
        }
        return {
            init: function () {
                metodo();
            }
        };
    }();

    jQuery(document).ready(function () {
       classe.init();
    });

First:

This is the correct way to instantiate classes/methods in javascript ? If not, how would the correct form be ?

Second:

JQuery methods are called cadentially:

$('#elemento').show().html('<html></html>').append('<div></div>').addClass('exemplo');

jQuery natively (without doing functions, only the original methods) can be considered an object orientation ?

  • jQuery methods give to chain because they all return one jQuery which is an object with the collection caught in the selector or changed if the last method has changed. You can confirm by consulting some of them in the documentation: show, append

  • @Anthraxis has already read in MDN documentation on classes in ES6?

  • @Andersoncarloswoss this specific link that I command not yet, but it is very strange how javascript works, use is easy, the difficult is to know what you are doing D:

2 answers

1

That is correct. But I would make the following observation. For instance, the new Classe().init() But it’s not the only way. You can also use as an aid, the prototype. will be useful to you.

With respect to object orientation. today I do not believe that js is OO. However, I believe that it makes use of objects and rhythmically, which enhances the use of language.

Today I believe that the great advantage in JS is in this "race" way of programming.

1


It’s interesting to think of J-query as an object-oriented javascript ally, but it makes no sense to mix J-query in things that aren’t related to DOM manipulation and event manipulation.

Think of things like inheritance, polymorphism, abstraction, encapsulation. J-query would only get in the way, because it is only a library that provides the methods of the native language.

In relation to class, there are also a multitude of modes of instantiation, each with its particularity.

Is it worth reading Unraveling the javascript language

Browser other questions tagged

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