Copy object properties in JavaScript -


i have custom object definitions like:

var class1 = function () { this.value = ''; }; var class2 = function () { this.data = ''; };  class1.prototype = {     setobject: function () {         (var prop in this){             if (typeof obj[prop] != 'undefined')                 this[prop] = obj[prop];         }     } }  class2.prototype = {     setobject: function () {         (var prop in this){             if (typeof obj[prop] != 'undefined')                 this[prop] = obj[prop];         }     } } 

is there way have method setobject default clases?

is better (simulate) inherit function object type in javascript or better use global function or define 1 one?

if you're going using library such jquery or underscore, you'll have access resilient extend method (see $.extend, , _.extend), there's no reason reinvent wheel on these custom object types.

otherwise, can have class1 , class2 inherit common base class:

function baseclass() {     ... } baseclass.prototype = {     setobject: function (obj) {...} };  function class1() {     ... } class1.prototype = new baseclass();  function class2() {     ... } class2.prototype = new baseclass();  var = new class1(); a.setobject({...});  var b = new class2(); b.setobject({...}); 

or, if objects should not contain common ancestor, define them use same setobject function reference:

function setobject(obj) {     ... }  function class1() {     ... } class1.prototype = {     setobject: setobject }; function class2() {     ... } class2.prototype = {     setobject: setobject } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -