Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.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对象属性 功能卡(styleAttr、cardInfo) { //属性 this.styleAttr=styleAttr; this.cardInfo=cardInfo; //功能 constructCard(this.styleAttr); } 函数构造卡(styleAttr){ var cardCSS={ “宽度”:styleAttr.width, “高度”:styleAttr.height, “背景色”:“黑色” } $('').appendTo('body').css(cardCSS); }_Javascript - Fatal编程技术网

在方法中访问Javascript对象属性 功能卡(styleAttr、cardInfo) { //属性 this.styleAttr=styleAttr; this.cardInfo=cardInfo; //功能 constructCard(this.styleAttr); } 函数构造卡(styleAttr){ var cardCSS={ “宽度”:styleAttr.width, “高度”:styleAttr.height, “背景色”:“黑色” } $('').appendTo('body').css(cardCSS); }

在方法中访问Javascript对象属性 功能卡(styleAttr、cardInfo) { //属性 this.styleAttr=styleAttr; this.cardInfo=cardInfo; //功能 constructCard(this.styleAttr); } 函数构造卡(styleAttr){ var cardCSS={ “宽度”:styleAttr.width, “高度”:styleAttr.height, “背景色”:“黑色” } $('').appendTo('body').css(cardCSS); },javascript,Javascript,嗨,这个卡片类得到了另外两个对象的as参数。其中之一是styleAttr,它包含一个名为“width”的属性。除非将此对象传递给constructCard,否则无法访问styleAttr.width属性。上面的例子有效。但如果我这样做: function Card(styleAttr, cardInfo) { //Attributes this.styleAttr = styleAttr; this.cardInfo = cardInfo; //Function

嗨,这个卡片类得到了另外两个对象的as参数。其中之一是styleAttr,它包含一个名为“width”的属性。除非将此对象传递给constructCard,否则无法访问styleAttr.width属性。上面的例子有效。但如果我这样做:

function Card(styleAttr, cardInfo)
{
    //Attributes
    this.styleAttr = styleAttr;
    this.cardInfo = cardInfo;

    //Functions


    constructCard(this.styleAttr);
}

function constructCard(styleAttr) {

    var cardCSS = {
                    'width':styleAttr.width,
                    'height':styleAttr.height,
                    'background-color':'black'
                  }


    $('<div class="Card"></div>').appendTo('body').css(cardCSS);
}
函数构造卡(){
var cardCSS={
“宽度”:this.styleAttr.width,//未定义
“高度”:styleAttr.height,
“背景色”:“黑色”
}
$('').appendTo('body').css(cardCSS);
}
主要是用其他语言编写的代码,所以我不确定,我必须将函数constructCard绑定到类才能访问它的属性,还是必须传递对象的属性才能获取值。还是我应该让它们成为全局变量

一定是一些简单的东西我没从Moz医生那里听懂

谢谢

试试:

function constructCard() {

    var cardCSS = {
                    'width': this.styleAttr.width, //Undefined
                    'height':styleAttr.height,
                    'background-color':'black'
                  }


    $('<div class="Card"></div>').appendTo('body').css(cardCSS);
}

普通旧原型继承没有问题:

var card = new Card(styleAttr, cardInfo);
card.construct();

看来这将是一个两步的对象构造。我的目标是通过一个新调用将所有内容设置一次,因为对象已经创建,但技术上还没有准备好。我的意思是,即使有文档记录,使用该对象所需的额外步骤听起来也不正确。实际上,你可能会说我应该在构造函数内部进行处理,但我有点想把它分离成一个函数,因为这会使构造函数膨胀。我得做个实验。谢谢你们,这更接近C语法,所以我更喜欢它。
var card = new Card(styleAttr, cardInfo);
card.construct();
function Card(styleAttr, cardInfo) {
    //Attributes
    this.styleAttr = styleAttr;
    this.cardInfo = cardInfo;
}

Card.prototype.constructCard = function () {

    var cardCSS = {
                    'width': this.styleAttr.width,
                    'height': this.styleAttr.height,
                    'background-color':'black'
                  };


    $('<div class="Card"></div>').appendTo('body').css(cardCSS);
}
var card_0 = new Card(..., ...)
card_0.constructCard();