Store and compare attribute value of an object in javascript

1

I have an object with some attributes and would like to store the value of one (the attribute is "state" and has value "closed") in a variable to compare its value by an "if" statement. What I'm looking for is doing something depending on the value of the "state" attribute.

I've tried something like that but it does not work:

var tipo = objeto.getAttribute('estado', 'cerrado');

if (tipo == 'cerrado') {

//ejecución

} else {

//ejecución

}

The object is the following:

// Pulsador Cerrado
var pulsador_cerrado = joint.shapes.basic.Generic.define('electrics.pulsador_cerrado', {
    size: {width: 80, height: 40},
    position: {x: 50},
    attrs: {
        '.': {magnet: false},
        '.body': {width: 50, height: 25},
        '.input': {ref: '.body', 'ref-x': -4, 'ref-y': 0.5, magnet: true, port: 'in'},
        '.output': {ref: '.body', 'ref-x': 84, 'ref-y': 0.5, magnet: true, port: 'out'},
        circle: {r: 5, stroke: 'black', fill: 'transparent', 'stroke-width': 1},
        image: {'xlink:href': 'pulsador.nc.jpg'},
        **estado: 'cerrado'**
    },
});

Do you know how to store the value of an attribute of an object and how to compare the value of that attribute with another value?

Pd: I'm using joint library and jquery.

Greetings and thanks

    
asked by Manuel Bueno Mora 10.05.2018 в 12:17
source

1 answer

1

In closed_button you have a class definition that you then have to instantiate according to what I have seen in the documentation. I understand that what you want to do would be as follows:

var pulsador_cerrado = joint.shapes.basic.Generic.define('electrics.pulsador_cerrado', {
    size: {width: 80, height: 40},
    position: {x: 50},
    attrs: {
        '.': {magnet: false},
        '.body': {width: 50, height: 25},
        '.input': {ref: '.body', 'ref-x': -4, 'ref-y': 0.5, magnet: true, port: 'in'},
        '.output': {ref: '.body', 'ref-x': 84, 'ref-y': 0.5, magnet: true, port: 'out'},
        circle: {r: 5, stroke: 'black', fill: 'transparent', 'stroke-width': 1},
        image: {'xlink:href': 'pulsador.nc.jpg'},
        estado: 'cerrado'
    },
});

//En base a lo que has definido creamos un objeto
var objeto= new pulsador_cerrado();

//Con esto obtienes el valor de estado
var tipo= objeto.attributes.attrs["estado"];

I hope it works for you

    
answered by 10.05.2018 / 13:35
source