简体   繁体   English

如何向函数添加方法?

[英]How to Add Methods to a Function?

There are many ways to call functions in JavaScript, but this just isn't working for me.在 JavaScript 中有很多方法可以调用函数,但这对我不起作用。 Could someone please tell me exactly what I'm doing wrong?有人可以告诉我我做错了什么吗?

I tried prototyping (eg gameObject.prototype = {}; ), but that didn't work for some reason.我尝试过原型设计(例如gameObject.prototype = {}; ),但由于某种原因没有奏效。 Now I'm just trying to assign the methods directly within the function, and that isn't even working.现在我只是想直接在函数中分配方法,这甚至不起作用。

What's wrong with this picture?这张图有什么问题?

function gameObject() {
    this.o = {};

    this.setimage = function(i) {
        this.o.img = i;
    };

    this.setDimensions = function(w, h) {
        this.o.width = w;
        this.o.height = h;
    };

    this.setPosition = function(x, y) {
        this.o.x=x;
        this.o.y=y;
    };

    this.create = function() {
        var el = document.createElement("div");
        el.className = "object " + this.o.cname;
        el.style.width = width * this.o.w;
        el.style.height = height * this.o.h;
        el.style.position = "absolute";
        el.style.top = height * this.o.y;
        el.style.left = width * this.o.x;
        map.appendChild(el);
    };

    this.setClass = function(c) {
        this.o.cname = c;
    };

    return this.o;
}

What I want is something like this:我想要的是这样的:

var d = new gameObject();
d.setClass("class");
d.setDimensions(0.8, 0.15);

I'm still fairly new to object oriented programming, so I don't even know if my vocabulary is correct.我对面向对象编程还很陌生,所以我什至不知道我的词汇是否正确。 What is it that I'm trying to do and what's the proper way to do it exactly?我正在尝试做什么以及正确的方法是什么?

You should not return anything from this constructor.你不应该从这个构造函数返回任何东西。

Remove this:删除这个:

return this.o;

Demo here .演示在这里

If you return a value from a constructor, the object created will of the type of the returned value.如果从构造函数返回一个值,则创建的对象将是返回值的类型。

Demo here .演示在这里 If you see this demo, da returns 4 means new gameObject returned the this.o value instead of this which is the gameObject() .如果你看到这个演示, da返回4意味着new gameObject返回this.o值而不是this ,即gameObject()

If you want to use prototype :如果你想使用prototype

function gameObject() {
    this.o = {};
}

gameObject.prototype = {
    setimage:function(i) {
        this.o.img = i;
    },
    setDimensions:function(w, h) {
        this.o.width = w;
        this.o.height = h;
    },
    setPosition:function(x, y) {
        this.o.x = x;
        this.o.y = y;
    },
    create:function() {
        var el = document.createElement("div");
        el.className = "object " + this.o.cname;
        el.style.width = width * this.o.w;
        el.style.height = height * this.o.h;
        el.style.position = "absolute";
        el.style.top = height * this.o.y;
        el.style.left = width * this.o.x;
        map.appendChild(el);
    },
    setClass:function(c) {
        this.o.cname = c;
    }
}

Demo here .演示在这里

In javascript, the best way to create instance methods is using a prototype.在 javascript 中,创建实例方法的最佳方式是使用原型。 This code should work:此代码应该工作:

function gameObject(){
    this.o={};
};
gameObject.prototype = {
    setimage: function(i){
        this.o.img=i;
    },
    setDimensions: function(w,h){
        this.o.width=w;
        this.o.height=h;
    },
    setPosition: function(x,y){
        this.o.x=x;
        this.o.y=y;
    },
    create: function(){
        var el=document.createElement("div");
        el.className="object "+this.o.cname;
        el.style.width=width*this.o.w;
        e.style.height=height*this.o.h;
        el.style.position="absolute";
        el.style.top=height*this.o.y;
        el.style.left=width*this.o.x;
        map.appendChild(el);
    },
    setClass: function(c){
        this.o.cname=c;
    }
};

The issue with how you were doing it before was returning something - you don't need to do that.你之前如何做的问题是返回一些东西——你不需要这样做。

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

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