布尔作为基元和布尔作为对象的属性有什么区别? [英] What's the difference between a boolean as primitive and a boolean as property of an object?

查看:89
本文介绍了布尔作为基元和布尔作为对象的属性有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注一些画布教程。下面的代码就是这个代码片段。

I'm following some canvas tutorial. The code below is a snippet of that.

在这个片段中,他们为什么不选择 runAnimation 来一个简单的布尔值?我认为 x =!x 语句无论如何都会起作用,但是当我尝试更改代码以使用布尔值时,代码不起作用。

In this snippet, why would they not choose for runAnimation to be a simple boolean? I would think the x = !x statement would work anyways, but when i tried changing the code to use booleans, the code didn't work.

那么,布尔作为基元和布尔作为对象的属性有什么区别?

So, what's the difference between a boolean as primitive and a boolean as property of an object?

   /*
   * define the runAnimation boolean as an object
   * so that it can be modified by reference
   */
  var runAnimation = {
    value: false
  };

  // add click listener to canvas
  document.getElementById('myCanvas').addEventListener('click', function() {
    // flip flag
    runAnimation.value = !runAnimation.value;


推荐答案

所有参数都在JavaScript中通过value传递。这意味着当传递参数时,会传递存储在变量中的副本。

基元(如布尔值)存储它们所代表的实际数据,因此,当传递基元时,会发送数据副本,从而产生两份数据副本。改变一个,不会影响另一个。

Primitives (like booleans) store the actual data they represent and so, when a primitive is passed, a copy of the data is sent, resulting in two copies of the data. Changes to one, won't affect the other.

但是,当你将一个对象分配给变量时,变量存储可以找到该对象的内存位置,而不是对象本身。将对象作为参数传递会导致传递内存地址的副本。在这些情况下,您可能会结束有两个存储相同内存地址的变量,所以无论你使用哪个变量,同一个底层对象都会受到影响。

But, when you assign an object to a variable, the variable stores the memory location for where that object can be found, not the object itself. Passing an object as an argument results in a copy of the memory address being passed. In these cases, you may wind up with two variables that store the same memory address, so no matter which variable you use, the same one underlying object is affected.

在你的场景中,你当然可以成功只使用一个布尔变量,但似乎教程想要将它封装到一个对象中,这样布尔数据的副本就不会浮动,并且意外更改一个变量而不是另一个变量的可能性会更小。

In your scenario, you could certainly make it work with just a boolean variable, but it appears that the tutorial wants to encapsulate that into an object so that copies of the boolean data won't be floating around and there will be less chances of accidentally changing one variable but not another.

以下是一些基本示例:

// This function takes a single argument, which it calls "input"
// This argument will be scoped to the function.
function foo(input){
  // The function is going to alter the parameter it received
  input = input + 77;
  console.log(input); 
}

var input = 100;  // Here's a higher scoped variable also called "input"

foo(input);       // We'll pass the higher scoped variable to the function

// Now, has the higher level scoped "input" been changed by the function?
console.log(input);  // 100 <-- No, it hasn't because primitives pass a copy of their data

// ************************************************

// Now, we'll do roughly the same thing, but with objects, not primitives
function foo2(obj){
  obj.someProp = 100;
  console.log(obj.someProp);
}

var obj = {
  someProp : 50
};

foo2(obj);

// Here, we see that the original object has been changed by the funciton we passed it to
console.log(obj.someProp);

这篇关于布尔作为基元和布尔作为对象的属性有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆