Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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

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 我们可以从jsonObject创建实例吗?_Javascript_Oop_Object - Fatal编程技术网

Javascript 我们可以从jsonObject创建实例吗?

Javascript 我们可以从jsonObject创建实例吗?,javascript,oop,object,Javascript,Oop,Object,我需要你的帮助,我的问题是我们可以从JsonObject创建一个实例吗 例如,流动的代码会产生错误 var player_hand = { x: null, y: null, height: null, width: null, style: null, set: function(x, y, width, height, style) { this.x= x; this.y= y; th

我需要你的帮助,我的问题是我们可以从JsonObject创建一个实例吗

例如,流动的代码会产生错误

var player_hand =
{
    x: null,
    y: null,
    height: null,
    width: null,
    style: null,
    set: function(x, y, width, height, style)  
    {
        this.x= x;
        this.y= y;
        this.width = width;
        this.height= height;
        this.style= style;

    },
    draw: function() 
    {
        DrawRect.draw(this.x, this.y, this.width, this.height , this.style);
    }
};
var DrawRect =
{
    draw: function(x, y, width, height, style)
    {
        gameContext.fillStyle = style;
        gameContext.fillRect(x, y, width, height);
    }
};

var left_hand = new player_hand(); // error.

我知道我的代码中的最后一行会导致错误,但是我们可以做类似的事情吗。

player\u hand
已经是一个Javascript对象,而不是构造函数

你需要这样做

function player_hand(...) 
{
    this.x = null;
    // ...
}
然后

var left_hand=新玩家_hand()

应该可以正常工作。

试试这个:

var player_hand = function(){
    return {
        x: null,
        y: null,
        height: null,
        width: null,
        style: null,
        set: function(x, y, width, height, style)  
        {
            this.x= x;
            this.y= y;
            this.width = width;
            this.height= height;
            this.style= style;

        },
        draw: function() 
        {
            DrawRect.draw(this.x, this.y, this.width, this.height , this.style);
        }
    }
};

然后可以使用
var left_hand=player_hand()

在javascript中,使用new Something()创建对象时,Something引用函数

如果要创建从对象player\u hand继承的实例,则需要声明一个函数,例如player\u hand()(约定是以大写字母开始构造函数名称),并将其原型设置为player\u hand:

function Player_hand() {}
Player_hand.prototype = player_hand;
你现在可以写:

var left_hand = new Player_hand();

严格地说,如果你很乐意告诉像InternetExplorer8或以下的人去哪里。。。实际上,您可以使用您的
player\u-hand
“definition”作为方法中的原型

简化示例:

var foo={
val:null,
getVal:函数(){
返回此.val;
}
},
bar=对象。创建(foo{
val:{value:'foo'}
});
console.log(bar.getVal());/'富

player\u hand
已经是一个Javascript对象,而不是一个构造函数。@thefourtheye你复制了我吗?@DanielA.White-Yup:-)我想我作为一个注释会更好看。希望你不介意。你的问题中没有JSON。实际上var left_hand=player_hand();更好。@Cerburs thanx对你非常有帮助。thanx对你非常有帮助。thanx pro,非常有帮助。