CanvasRenderingContext2D的Lis属性

时间:2016-01-26 13:19:39

标签: javascript canvas

您好我正在尝试列出CanvasRenderingContext2D对象的所有方法和属性。我在列表方法中取得了成功,但是当我尝试列出属性时,我得到了奇怪的结果。我得到的只是一个重复多次的相同属性的数组。任何人都可以帮助我吗?

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>

1 个答案:

答案 0 :(得分:1)

这一行(在声明属性变量之后):

for(var i = 0; i < methods.length; i++) {

应该是:

for(var i = 0; i < properties.length; i++) {

您应该循环方法具有值的次数,何时应该循环属性

这一行也是:

for (var key in ctx) {
        if (Object.getOwnPropertyNames(ctx)) {
            properties.push(m);
            properties.join(',');
        }
    }

你可能正在推动m变量,或许你想要推动关键&#39;