Moris' Note Book

本博客所有内容皆收集于网上,非本人原创,非心情日记,非研究心得,只是自己浏览的东西的收集
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

notice: javascript's by value and by reference

Posted on 2006-04-07 11:02  moris  阅读(149)  评论(0)    收藏  举报

Primitive Types and Reference Types
 By value versus by reference
 

By value

By reference

Copy

The value is actually copied; there are two distinct, independent copies.

Only a reference to the value is copied. If the value is modified through the new reference, that change is also visible through the original reference.

Pass

A distinct copy of the value is passed to the function; changes to it have no effect outside the function.

A reference to the value is passed to the function. If the function modifies the value through the passed reference, the modification is visible outside the function.

Compare

Two distinct values are compared (often byte by byte) to see if they are the same value.

Two references are compared to see if they refer to the same value. Two references to distinct values are not equal, even if the two values consist of the same bytes.

Numbers and booleans are primitive types in JavaScript

Object and Array are reference types

String : when pass as arg in function, I dont care, because you have not way to modi the string, when it is compare, it is by value. when it is copied, by value too.