te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - js Change variable will affect to original variable - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - js Change variable will affect to original variable - Stack Overflow

programmeradmin4浏览0评论

I am sorry I do not know how to explain, but the situation is like this example.

After I instant a new function c, and change the value this.i will directly affect to a.

How can I solve this issue?? I do no know change this.i will affect to a.

var a = { c: 1 };

var b = function(){
  this.i = a;

  this.i.c = 2;
};


var c = function(){
   this.i = a;

   alert(this.i.c);

};

c.prototype.set = function(){
  this.i.c = 4;

  alert(a.c);
};

d =new c();

d.set();

I am sorry I do not know how to explain, but the situation is like this example.

After I instant a new function c, and change the value this.i will directly affect to a.

How can I solve this issue?? I do no know change this.i will affect to a.

http://jsbin./iPIkomu/1/edit

var a = { c: 1 };

var b = function(){
  this.i = a;

  this.i.c = 2;
};


var c = function(){
   this.i = a;

   alert(this.i.c);

};

c.prototype.set = function(){
  this.i.c = 4;

  alert(a.c);
};

d =new c();

d.set();
Share Improve this question asked Aug 18, 2013 at 21:07 MicahMicah 4,5608 gold badges32 silver badges40 bronze badges 3
  • 2 If you don't want the object to be shared, then why are you making reference to the same object on every object created by your constructor? Was there some other purpose for the var a = {...}? Is there a reason it needs to be outside the constructor? I can't tell how to solve the issue because I don't know what you're ultimately trying to do. – user2437417 Commented Aug 18, 2013 at 21:10
  • That is because a could be a default set of variables used for many constructor..will that make sense? – Micah Commented Aug 18, 2013 at 21:24
  • 1 If it's to be used by several constructors, and you don't want to repeat the code, then either create it in a function as shown in the answer below, or use the shared object as a prototype of the main objects, and use Object.create() to set up the inheritance. So you'd do this.i = Object.create(a); – user2437417 Commented Aug 18, 2013 at 21:28
Add a ment  | 

4 Answers 4

Reset to default 9

Use this

let person1 = { name: 'Vitor', birthYear: 1995 };
// ES6 method
let person2 = Object.assign({}, person1);

In case of ArrayObject

let person1 = [{ name: 'Vitor', birthYear: 1995 },
              { name: 'Mark', birthYear: 1998 }];
// ES6 method
let person2 = Object.assign([], person1);

Refere to: https://hackernoon./javascript-reference-and-copy-variables-b0103074fdf0

Objects are always passed around by reference. a and this.i refer to the exact same object.

To get different objects, you'd need to just do this.i = {c:1}; or similar.

I'll suggest to change:

var a = { c: 1 };
to
var a = function() { return { c: 1 }; }

and then later

this.i = a;
to
this.i = a();

Doing the things like that you will be sure that you always get a new object. Using your code you are referring same object.

Since "a" is an object, therefore by equating them it passes the reference to "a", thereby making changes in both the variables. You can use any of the 2 methods -

this.i = JSON.parse(JSON.stringify(a));

OR

this.i = Object.assign({}, a);

发布评论

评论列表(0)

  1. 暂无评论