最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript passing variable as value and not as reference - Stack Overflow

programmeradmin1浏览0评论

My problem is simple I think, but I couldn't find a simple solution to it. Here is the example:

var obj1 = {
    m1:"a",
    m2:"b" 
};

var obj2 = {
    m:obj1
};


obj2.m.m1 = "c";


document.write(obj2.m.m1+"<br>"); //output: c

document.write(obj1.m1+"<br>"); // output: c ( I wanted to be a)

So.. what do I need to do to return "a" from obj1.m1 ?

Thanks in advance

My problem is simple I think, but I couldn't find a simple solution to it. Here is the example:

var obj1 = {
    m1:"a",
    m2:"b" 
};

var obj2 = {
    m:obj1
};


obj2.m.m1 = "c";


document.write(obj2.m.m1+"<br>"); //output: c

document.write(obj1.m1+"<br>"); // output: c ( I wanted to be a)

So.. what do I need to do to return "a" from obj1.m1 ?

Thanks in advance

Share Improve this question edited Apr 28, 2011 at 20:01 Herman Schaaf 48.5k21 gold badges105 silver badges140 bronze badges asked Apr 28, 2011 at 19:58 Doua BeriDoua Beri 10.9k18 gold badges95 silver badges143 bronze badges 2
  • possible duplicate of javascript how to create reference – Quentin Commented Apr 28, 2011 at 19:59
  • @David Dorwad actually I'm looking for the opposite thing. How to pass objects as value and not as refference – Doua Beri Commented Apr 28, 2011 at 20:02
Add a ment  | 

4 Answers 4

Reset to default 6

You need to set obj2.m to a clone of obj1, not obj1 itself. For instance:

function clone(obj) {
    var result = {};
    for (var key in obj) {
        result[key] = obj[key];
    }
    return result;
}

var obj2 = {
    m: clone(obj1)
};

obj2.m.m1 = "c";  // does not affect obj1.m1

This may be of use:

http://my.opera./GreyWyvern/blog/show.dml/1725165

obj1 and obj2.m point to the same object. You cannot have obj1.m1 != obj2.m.m1

What you can do is assign a copy of obj1 to obj2.m. See the link Will posted.

you need to clone it first.

Object.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};

var obj1={    m1:"a",    m2:"b" };
var obj2={    m: {}};
obj2.m = obj1.clone();
obj2.m.m1="c";
document.write(obj2.m.m1+"<br>"); 
document.write(obj1.m1+"<br>"); 
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>