I’ve been using jQuery.extend to replace default properties like this
var Car = function(options){
var defaultOptions = {
color: "hotpink",
seats: {
material: "fur",
color: "black",
count: 4
},
wheels: 4
}
this.options = $.extend(true,{},defaultOptions,options);
}
var myCar = new Car({
color: "blue",
seats: {
count: 2,
material: "leather"
}
});
alert(myCar.options.color); // "blue"
alert(myCar.options.seats.color); // "black"
alert(myCar.options.seats.count); // 2
While it works great, I’d like to know the best way to achieve similar results without any libraries. I just want to define some default settings in a function and replace them with settings from arguments, would be an overkill to include a library every time I do that.
Basically it’s just a recursive use of
for..in. You can see the full source of jQuery’s implementation in the source code (the line number on that will rot over time, but it’s likely to remain incore.js).Here’s a very basic off-the-cuff: