Reputation: 325
Hi I'm trying to list all of methods and properties of CanvasRenderingContext2D object. I successeed in listing methods however I get strange results when I try to list properties. All I get is ana array of the same property repeated multiple times. Anyone can help me with that?
var canvas = document.querySelector('#canvas');
var ctx = canvas.getContext('2d');
var methods = [];
for (var m in ctx) {
if (typeof ctx[m] == "function") {
methods.push(m);
methods.join(',');
}
}
var methodsContainer = document.querySelector('#methods');
for(var i = 0; i < methods.length; i++) {
var opt = methods[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
methodsContainer.appendChild(el);
}
var properties = [];
for (var key in ctx) {
if (Object.getOwnPropertyNames(ctx)) {
properties.push(m);
properties.join(',');
}
}
var propertiesContainer = document.querySelector('#properties');
for(var i = 0; i < methods.length; i++) {
var opt = properties[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
propertiesContainer.appendChild(el);
}
<canvas id="canvas"></canvas>
<h5>Methods</h5>
<select id="methods">
</select>
<br/>
<h5>Properties</h5>
<select id="properties">
</select>
Upvotes: 0
Views: 150
Reputation: 381
This line (after declaring the properties variable):
for(var i = 0; i < methods.length; i++) {
Should be:
for(var i = 0; i < properties.length; i++) {
You're for looping the amount of times the method has values, when you should be looping properties
This line also:
for (var key in ctx) {
if (Object.getOwnPropertyNames(ctx)) {
properties.push(m);
properties.join(',');
}
}
You're pushing the m variable, when maybe you're intending to push 'key'
Upvotes: 1