qarthandso
qarthandso

Reputation: 2190

Simple JavaScript Object Constructor

I'm reading a beginner's guide on machine learning from scratch with JavaScript.

About a 1/4 way down the page is the section titled "THE CODE". Right under that section heading is the code in question.

var Node = function(object) {  
    for (var key in object)
    {
        this[key] = object[key];
    }
};

I realize this could be a very basic constructor function but I've never seen this pattern before.

Is there any links or guides about this pattern design or type of constructor. I'd like to learn as much as possible as I can about it.

Upvotes: 1

Views: 54

Answers (1)

user663031
user663031

Reputation:

There's not much to learn about or understand. It's simply constructing a new object and copying the properties from some other object into it.

In modern JS, you could also write

function Node(object) {
  Object.assign(this, object);
}

Upvotes: 1

Related Questions