Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向函数添加方法?_Javascript_Oop_Function_Methods - Fatal编程技术网

Javascript 如何向函数添加方法?

Javascript 如何向函数添加方法?,javascript,oop,function,methods,Javascript,Oop,Function,Methods,在JavaScript中调用函数的方法有很多,但这对我来说并不适用。有人能确切地告诉我我做错了什么吗 我尝试了原型设计(例如,gameObject.prototype={};),但由于某些原因,这不起作用。现在我只是试图直接在函数中分配方法,但这根本不起作用 这幅画怎么了 function gameObject() { this.o = {}; this.setimage = function(i) { this.o.img = i; }; t

在JavaScript中调用函数的方法有很多,但这对我来说并不适用。有人能确切地告诉我我做错了什么吗

我尝试了原型设计(例如,
gameObject.prototype={};
),但由于某些原因,这不起作用。现在我只是试图直接在函数中分配方法,但这根本不起作用

这幅画怎么了

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;
}
我想要的是这样的东西:

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

我对面向对象编程还是相当陌生的,所以我甚至不知道我的词汇是否正确。我要做的是什么?正确的方法是什么?

你不应该从这个构造函数返回任何东西

删除此项:

return this.o;


如果从构造函数返回值,则创建的对象将具有返回值的类型

。 如果您看到此演示,
d.a
返回
4
意味着
新游戏对象
返回了
this.o
值,而不是
this
,后者是
游戏对象()

如果要使用
原型

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;
    }
}

.

您不应从该构造函数返回任何内容

删除此项:

return this.o;


如果从构造函数返回值,则创建的对象将具有返回值的类型

。 如果您看到此演示,
d.a
返回
4
意味着
新游戏对象
返回了
this.o
值,而不是
this
,后者是
游戏对象()

如果要使用
原型

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;
    }
}

在javascript中,创建实例方法的最佳方法是使用原型。此代码应适用于:

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;
    }
};

之前如何执行的问题是返回一些东西-您不需要这样做。

在javascript中,创建实例方法的最佳方法是使用原型。此代码应适用于:

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;
    }
};

你以前是如何做的问题是返回一些东西-你不需要这样做。

为什么你要从构造函数返回
this.o
?我会放弃它,你的代码应该可以工作。顺便说一句,你希望使用多少游戏对象?这是一个变量,考虑到我想让用户创建自己的游戏对象。你为什么从构造函数返回
这个.o
?我不想说了,你的代码应该可以用。顺便说一句,你希望使用多少游戏对象?这是一个可变的数量,考虑到我想让用户创建自己的游戏对象。这不应该是一个通用的声明;在这种情况下,不应从构造函数返回任何不应是此类泛型语句的内容:);在这种情况下,不应从构造函数返回任何内容:)