Node.js: javascript PLO, why such designs?
-
Started studying Node.js and often sees such designs:
var Network = function(url) { this.url = url; }
Why are they taking their current properties inside?
-
Network is probably a design function (generally class). A copy of the class will be created later. If the url is available to all methods of the new facility, the variable url must be recorded in the object ' s properties. Try the next code and see what it is.
Network = function(url) { this.url = url; } Network.prototype = { testProperties: function() { console.log(typeof url); // undefined; console.log(this.url); // ru.stackoverflow.com } }; var stackoverflow = new Network('ru.stackoverflow.com'); stackoverflow.testProperties();