如何获得对象的价值

时间:2015-09-08 13:13:50

标签: javascript jointjs

我获取元素位置属性并将其存储在变量中。当我在控制台中打印该变量时,我可以看到它是一个像这样的对象:

Object {.portsTool path.aggregate: Object, .portsTool rect.remove: Object, .portsTool .handleInPorts: Object, .portsTool .handleOutPorts: Object, .resizeTool .resize: Object…}.: Object.body: Objectheight: 250stroke: "#000000"width: 150__proto__: Object.inPorts .port-label: Object.inPorts circle: Object.moveTool .area: Object.moveTool .visual: Object.outPorts .port-label: Object.outPorts circle: Object.port-body: Object.portsTool .handleInPorts: Object.portsTool .handleOutPorts: Object.portsTool path.aggregate: Object.portsTool rect.remove: Object.resizeTool .resize: Objectrect: Objecttext: Object__proto__: Object

当我扩展它时:

>: Object
.body: Object
height: 250
stroke: "#000000"
width: 150
__proto__: Object

我希望从中获得宽度的值。我怎么做?我是javascript的新手

更新

var border = border2.prop('attrs');
   for (y in border) 
   {
     console.log(border[y]);
   }

使用上面的迭代,它打印像这样的对象:

Object {d: "m0,5l5,0l0,-5l5,0l0,5l5,0l0,5-5,0l0,5l-5,0l0,-5l-5,0z", stroke-width: 2, stroke: "#000", fill: "#5F5"}
(index):527 Object {width: 15, height: 6, stroke-width: 3, stroke: "#000", fill: "#F55"…}
(index):527 Object {ref: ".body", ref-x: -30, ref-y: -40}
(index):527 Object {ref: ".body", ref-dx: 10, ref-y: -40}
(index):527 Object {d: "M 0,10l10,0l0,-10z M -2,13l15,0l0,-14l0,14", fill: "black", stroke: "black", ref: ".body", ref-dx: 4…}
(index):527 Object {d: "M 0,15l5,-3l0,6l-5,-3l30,0l-5,-3l0,6l5,-3l-15,0l0,15l-3,-5l6,0l-3,5l0,-30l-3,5l6,0l-3,-5l0,15", fill: "black", stroke: "black", ref: ".body", ref-x: -40…}
(index):527 Object {ref: ".moveTool .visual", ref-x: 0, ref-y: 0, cx: "15", cy: "15"…}
(index):527 Object {font-size: 14, text: "", ref-x: 0.5, ref-y: 0.5, ref: ".body"…}
(index):527 Object {stroke: "#fff", fill: "#F9F9F9", stroke-width: 1, opacity: 0.8}
(index):527 Object {magnet: false, fill: "#FFFFFF", stroke: "none"}
(index):527 Object {type: "input"}
(index):527 Object {type: "output"}
(index):527 Object {r: 3, magnet: true, stroke: "#000000"}
(index):527 Object {width: 150, height: 250, stroke: "#000000"}
(index):527 Object {x: -15, dy: 4, text-anchor: "end", fill: "#000000"}
(index):527 Object {x: 15, dy: 4, fill: "#000000"}

5 个答案:

答案 0 :(得分:2)

解决方案是获取对象属性的名称。您想获取对象portsTool的宽度属性。 所以你可以使用:

portsTool.width

为了扩展对象以查看你将如何调用它,你可以这样做:

for (x in myObj) {
    console.log(myObj[x]);
}

答案 1 :(得分:1)

“width”是对象的属性,可以这样访问:{"A write operation resulted in an error.\r\n The positional operator did not find the match needed from the query. Unexpanded update: p2l.$.status"}

答案 2 :(得分:1)

假设您的对象存储在myObj中,您可以使用

访问该属性
myObj.width

myObj["width"]

答案 3 :(得分:0)

宽度是对象的属性,因此您只需使用名为后跟.width的变量引用它。

答案 4 :(得分:0)

名为border的变量是JointJS元素,对吗?如果是这样,你可以得到它的宽度和高度:

var theWidth = border.attributes.size.width;
var theHeight = border.attributes.size.height;