简体   繁体   English

原型 function vue.js

[英]Prototype function vue.js

Need to implement a function prototype on Vue.js the third day I can't find how to do it.需要在Vue.js上实现一个function原型,第三天找不到怎么做。 From the prototype I can't reach the "parent" variables从原型我无法到达“父”变量

function Shape(x, y, w, h, fill) {
    this.x = x || 0;
    this.y = y || 0;
    this.w = w || 1;
    this.h = h || 1;
    this.fill = fill || '#AAAAAA';
}

Shape.prototype.draw = function(ctx) {
    ctx.fillStyle = this.fill;
    ctx.fillRect(this.x, this.y, this.w, this.h);

    ctx.fillStyle = '#AAAAAA'
    ctx.fillRect(this.x + 10, this.y + 10, this.w - 20, this.h - 20, 20);
}

Shape.prototype.contains = function(mx, my) {
    return (this.x <= mx) && (this.x + this.w >= mx) &&
        (this.y <= my) && (this.y + this.h >= my);
}

This is the closest to the goal, but it draws but does not see variables这是最接近目标的,但它绘制但看不到变量

exports.Shape = (x1, y1, w1, h1, fill1) => {
    this.x = x1 || 0;
    this.y = y1 || 0;
    this.w = w1 || 1;
    this.h = h1 || 1;
    this.fill = fill1 || '#AAAAAA';
};

exports.Shape.prototype.draw = function(ctx, Obj) {
    ctx.fillStyle = '#FF00FF';
    ctx.fillRect(50, 50, 50, 50);

    ctx.fillStyle = this.fill;
    ctx.fillRect(this.x, this.y, this.w, this.h);

    ctx.fillStyle = '#AAAAAA'
    ctx.fillRect(this.x + 10, this.y + 10, this.w - 20, this.h - 20, 20);
}

exports.Shape.prototype.contains = function(mx, my) {
    return (this.x <= mx) && (this.x + this.w >= mx) &&
        (this.y <= my) && (this.y + this.h >= my);
};

but this.x is undefined但是 this.x 是未定义的

I don't see a problem with your first example?我看不出你的第一个例子有问题吗? The variables can be reached, I log them here in the console.可以访问变量,我将它们记录在控制台中。

Maybe you forgot to define ctx globally?也许你忘了全局定义ctx

 function Shape(x, y, w, h, fill) {
    this.x = x || 0;
    this.y = y || 0;
    this.w = w || 1;
    this.h = h || 1;
    this.fill = fill || '#AAAAAA';
    
    this.draw()
}

Shape.prototype.draw = function(ctx) {
    console.log("variables of Shape:")
    console.log(this.x)
    console.log(this.fill)
}

let s = new Shape(30,30,30,30,"#EEF")
s.draw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM