Skip to content
/ pyoo.js Public

Orientação a Objetos no estilo Python para JavaScript

License

Notifications You must be signed in to change notification settings

wolmir/pyoo.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

May 15, 2018
a8fbe5a · May 15, 2018

History

29 Commits
May 15, 2018
May 15, 2018
Sep 6, 2016
Aug 31, 2016
Apr 7, 2016
May 15, 2018
May 15, 2018
May 15, 2018
May 15, 2018
Apr 9, 2016
May 15, 2018

Repository files navigation

pyoo.js

Uma biblioteca JavaScript que oferece orientação a objetos no estilo Python.

Documentação

Instalação

Node

npm install pyoo.js

Para usar ela no browser sem Webpack:

<script src="node_modules/pyoo.js/dist/pyoo.js"></script>

Classes

var CExemplo = Classe({
    __init__: function(self) {
        self.attr1 = 'Olá';
        self.attr2 = 'Mundo';
    },

    metodoExemplo: function(self) {
        return self.attr1 + ', ' + self.attr2 + '!';
    }
});

var objExemplo = CExemplo();
console.log(objExemplo.metodoExemplo());

Herança

As classes suportam múltipla herança e múltiplos níveis de herança.

var Animal = Classe({
    __init__: function(self, nome) {
        self.nome = nome;
    },

    falar: function(self) {
        return 'Olá, meu nome é ' + self.nome + '!';
    }
});


var Reptil = Classe(Animal, {
    __init__: function(self, nome) {
        Animal.__init__(self, nome);
    },

    falar: function(self) {
        var fala_animal = Animal.falar(self);
        return fala_animal + ' Eu sou um réptil!'
    }
});

Exemplo

Python

class Foo:
    def __init__(self, b):
        self.a = b

    def metodo1(self, c):
        return self.a + c

j = Foo(6)

# Escreve 10
print j.metodo1(4)

JavaScript

var Foo = Classe({
    __init__: function(self, b) {
        self.a = b;
    },

    metodo1: function(self, c) {
        self.a = 3
        return(self.a + c);
    }
});

var j = Foo(6)

// Escreve 10
console.log(j.metodo1(4));

Funcionalidades Futuras

  • Variáveis privadas
  • isinstance()
  • issubclass()
  • Decoradores

About

Orientação a Objetos no estilo Python para JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published