Writing properties to a cookie
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 request 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开发者_Python百科 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.
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.
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.
精彩评论