English 中文(简体)
书写 to具
原标题:Writing properties to a cookie
  • 时间:2010-01-29 05:47:55
  •  标签:
  • javascript

要求我建立一种职能,记载通过功能传给 co的“类型”的财产。 我从未使用过足以理解的java文字,而这一特别请求确实是特别的,我一直在寻求对类似守则的解释,时间超过8小时。

如果我宣布以下变量:

var types = {
    "sugar" : { "color" : "blue", "weight" : 1200, "decoration" : "frosting"},
    "chocolate chip" : { "color" : "brown", "weight" : 12, "chocolateType" : "milk"}
}; 

以及产品重量的以下功能。

CookieBase.prototype.getWeight = function() { return this.weight; };

既然实际上已经宣布,我怎么会写信给厨师。 在加权功能中的this是变型的吗?

这里是整个法典的部分:

function CookieBase() {}
CookieBase.prototype.getWeight = function() { return this.weight; };

var CookieFactory = function(){
    var types = {
        "sugar" : { "color" : "blue", "weight" : 1200, "decoration" : "frosting"},
        "chocolate chip" : { "color" : "brown", "weight" : 12, "chocolateType" : "milk"}
    };
    return {};
}();

我不想看守则本身,我真的希望有人向我解释这一概念。 这是对工作进行筛选的过程,因此,我想给他们自己的法典,但我不对这一概念表示怀疑。

这里的问题是他们想要的:

在库克群岛,采用一种称为“面包车”的公开方法,采用单一参数——类型。 这种方法应当创立一个与所申请的 co基类特性相关的 co基。 如果不能确定这种类型的回报无效。 该法典不应超过10条线。

最佳回答

为了在物体上设定任意财产,你可以使用彩票:

thing.setProperty = function (type, val) {
    this[type] = val;
}

也可使用<<<>t>for的物体的财产名称:

var msg=  ;
for (p in thing) {
     msg += p +  :   + thing[p] +  
 ;
}
if (console && console.log) {
    console.log(msg);
} else {
    alert(msg);
}

没收这些物品,并复制一个物体的特性。 这使你获得实施mixins的多数途径,这些途径与他们再次提出的要求一样。

问题回答

查阅您对https://stackoverflow.com/questions/2160144/even-properties-to-a-cookie/2160198#2160198>outis, 如你想要执行CookieFactory。 反对:

// assuming that the CookieBase constructor is declared
var CookieFactory = (function () {
  var types = {
    "sugar" : {"color" : "blue", "weight" : 1200, "decoration" : "frosting"},
    "chocolate chip" : {"color" : "brown", "weight" : 12,
                        "chocolateType" : "milk"}
  }; 

  return { // public interface
    bakeCookie: function(type){
      var cookie = new CookieBase(),
          cookieType = types[type];

      if (!cookieType) return null; // no type found, return null

      for(var prop in cookieType) 
        if (cookieType.hasOwnProperty(prop))
          cookie[prop] = cookieType[prop];

      return cookie;
    }
  };
})();

var myCookie = CookieFactory.bakeCookie( sugar );
// Object color=blue weight=1200 decoration=frosting
alert(myCookie.getWeight()); // 1200

页: 1 方法将产生<条码>CookieBase 反对,并将copy通过反对理由的特性。

由于归还的物体是用<代码>CookieBase建造商创建的,你可以查阅<代码>上申报的所有财产。 CookieBase.prototype on that sent Object.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签