tags:

views:

54

answers:

2

I was asked to create a function that writes the property of a "type" that is send through a function to a cookie. I have never used javascript enough to understand it and this particular requestion is really particular I have been looking for an explaination of similar code for over 8 hours.

If I have the follow variable declared:

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

And the following function which returns the weight of the product.

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

How would I write to a cookie the properties of any type sent to the function, given it was actually declared. Would I be correct that the this in the getweight function is the types variable?

Here is the entire code fragment:

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 {};
}();

I am not looking for the code itself I would really like somebody to explain the concept to me. This is for a screening process for a job, so I want to give them my own code, but I am not familar with this concept.

Here is the question and what they want exactly:

In CookieFactory implement a public method named bakeCookie that takes a single parameter -- type. This method should create a cookie base instance with properties of the requested cookie type appended to it. If the type cannot be created return null. This code should not be more than about 10 lines long.

+2  A: 

To set an arbitrary property on an object, you can use the bracket syntax:

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

You can also loop over the property names of an object using for:

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

Combine those and you can copy the properties of one object to another. This gets you most of the way to implementing mixins, which sounds like what they're asking for.

outis
This does not answer my question, or I should say the question that was asked of me. Although the code you provided does make it look more familar. They want a function that accepts a type and then writes the properties of the type to a cookie.What I am unable to figure out is how to write a generic function that will write the properties to any type defined.
Ramhound
Looks a little like mixins (http://en.wikipedia.org/wiki/Mixin). At least it did.
outis
Here is the question and what they want exactlyIn CookieFactory implement a public method named bakeCookie that takes a single parameter -- type. This method should create a cookie base instance with properties of the requested cookie type appended to it. If the type cannot be created return null. This code should not be more than about 10 lines long.I would rather provide me a general explaination or do what outis did and provide me a better peice of code that allows me to figure out what they actually want.
Ramhound
No such thing as `foreach` in JavaScript. It's just `for (p in thing) {`.
Tim Down
@Tim: *that's* right. Too much PHP and C# recently.
outis
+1  A: 

By looking at the last comment you made to the answer by outis, sounds like you want to implement a CookieFactory object like this:

// 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

As you notice the bakeCookie method will create a CookieBase object, and it will copy the properties of the object argument passed to it.

Since the object returned was created with the CookieBase constructor, you can access all the properties declared in the CookieBase.prototype on that returned object.

CMS
Way to give away the cow ;-)
outis
@outis: I never heard that expression before, could you translate? :)
CMS
He means you gave away the answer, sadly the usage of "types['sugar']" is now how the use the function.Here is what how the function should work:The following code snippet will execute properly with the code…var sugarCookie = CookieFactory.bakeCookie("sugar");var almondCookie = CookieFactory.bakeCookie("almond");
Ramhound
You answered my question on how to loop through the properties. Which solves half the problem. I should be able to figure out how to combine each property into a single string. The only question is if the way you said to use the argument is going to be a problem.
Ramhound
@CMS: it's a combination of the ironic statement "way to ..." and "why buy the cow when you can get the milk for free?" whose literal meaning doesn't apply, but works as 2-degrees of metaphor.
outis
@Ramhound, edited my example to match your API requirements... @outis, got it!
CMS
You are a big help.This will give me a great start.
Ramhound
CMS does it matter the code they gave me did not use CookieBase has on object in the typical sense? I come from a huge pure C++ and C# background. So some of the structure of JavaScript is hard to understand
Ramhound
I guess what I am asking the fact everything is a function confuses me :$
Ramhound