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; } ?>class - Javascript passing object to function - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

class - Javascript passing object to function - Stack Overflow

programmeradmin3浏览0评论

Quick question on Javascript to which I can't find a clear concise answer.

I'm building an app that's way ahead of anything I've done before and involves multiple classes being instantiated. These objects are then passed into a processing class that checks user inputs, draws onto canvas and updates the objects that it has been passed.

I am wondering, how does JavaScript handle passing objects to functions? Am I passing a copy of the object, or am I passing a reference to the object?

So if my controller class alters one of the objects variables, is that changed everywhere or just in the object that that controller sees?

Sorry for such a simple, possibly easily testable question but I'm not even sure if I'm making a class correctly at this point thanks to errors piling up.

Quick question on Javascript to which I can't find a clear concise answer.

I'm building an app that's way ahead of anything I've done before and involves multiple classes being instantiated. These objects are then passed into a processing class that checks user inputs, draws onto canvas and updates the objects that it has been passed.

I am wondering, how does JavaScript handle passing objects to functions? Am I passing a copy of the object, or am I passing a reference to the object?

So if my controller class alters one of the objects variables, is that changed everywhere or just in the object that that controller sees?

Sorry for such a simple, possibly easily testable question but I'm not even sure if I'm making a class correctly at this point thanks to errors piling up.

Share Improve this question edited Jan 31, 2017 at 19:06 SCGH 9678 silver badges14 bronze badges asked Jun 28, 2013 at 8:53 Chris MorrisChris Morris 9832 gold badges18 silver badges41 bronze badges 11
  • google./… – C3roe Commented Jun 28, 2013 at 8:55
  • You're passing a reference. Assignments obj1 = obj2 also just create more references to the same object. – nnnnnn Commented Jun 28, 2013 at 8:56
  • in javascript objects are passed by reference – user405398 Commented Jun 28, 2013 at 8:57
  • 1 Did you see this answer? I see your pain as the answers are ambiguous but this cleared things up for me. Best bet is to do a bit of experimenting with jsFiddle for clarity :-) – Stokedout Commented Jun 28, 2013 at 9:02
  • Thanks for the link, I'm not too sure of the syntax but it does illustrate the way i;ve been doing it in the second part and nice to know it is being done correctly. 200 lines of code and 4 hours last night weren't wasted. – Chris Morris Commented Jun 28, 2013 at 9:08
 |  Show 6 more ments

3 Answers 3

Reset to default 4

When passing in a primitive type variable like a string or a number, the value is passed in by value. This means that any changes to that variable while in the function are pletely separate from anything that happens outside the function.

function myfunction(x)
{
      // x is equal to 4
      x = 5;
      // x is now equal to 5
}

var x = 4;
alert(x); // x is equal to 4
myfunction(x); 
alert(x); // x is still equal to 4

Passing in an object, however, passes it in by reference. In this case, any property of that object is accessible within the function

function myobject()
{
    this.value = 5;
}
var o = new myobject();
alert(o.value); // o.value = 5
function objectchanger(fnc)
{
    fnc.value = 6;
}
objectchanger(o);
alert(o.value); // o.value is now equal to 6

As well described in https://stackoverflow./a/5314911/636348, in JavaScript it's always pass by value, but for objects the value of the variable is a reference.

So, it's not a pure pass by reference. Here's an example to understand this concept:

E.g.:

x = {member:"foo"}

If you change the object with another object inside a function, you won't get the new object outside the function scope because you just create another object. The original reference is still bound to the original object:

function changeObject(x) {
  x = {member:"bar"};
}

changeObject(x);
alert(x.member)

output: foo

instead, if you alter a member inside a function, the object will be changed:

function changeMember(x) {
  x.member = "bar";
}

changeMember(x);
alert(x.member)

output: bar

If you pass in a variable which is pointing to an object, it passes a reference to the object. If you pass in an object literal, then obviously no other class or function will be able to change that object.

发布评论

评论列表(0)

  1. 暂无评论