Asked  7 Months ago    Answers:  5   Viewed   18 times

I read a book called "Professional Javascript for web developer" and it says: "Variable is assigned by Reference value or Primitive Value. Reference values are objects stored in memory". And then it says nothing about how Primitive value is stored. So I guess it isn't stored in memory. Based on that, when I have a script like this:

var foo = 123;

How does Javascript remember the foo variable for later use?

 Answers

79

A variable can hold one of two value types: primitive values or reference values.

  • Primitive values are data that are stored on the stack.
  • Primitive value is stored directly in the location that the variable accesses.
  • Reference values are objects that are stored in the heap.
  • Reference value stored in the variable location is a pointer to a location in memory where the object is stored.
  • Primitive types include Undefined, Null, Boolean, Number, or String.

The Basics:

Objects are aggregations of properties. A property can reference an object or a primitive. Primitives are values, they have no properties.

Updated:

JavaScript has 6 primitive data types: String, Number, Boolean, Null, Undefined, Symbol (new in ES6). With the exception of null and undefined, all primitives values have object equivalents which wrap around the primitive values, e.g. a String object wraps around a string primitive. All primitives are immutable.

Tuesday, June 1, 2021
 
Fanda
answered 7 Months ago
15

This has nothing to do with the type of the field you are changing. What is relevant here is indeed just the kind of the parent type:

IntHolder first = new IntHolder();
IntHolder second = first;

Depending on the kind of IntHolder this has two different effects:

For value types (struct), this creates a copy. The data of the value type object lives with the object, so all of it is copied. It’s kind of equivalent to this:

IntHolder second = new IntHolder();
second.number = first.number;
// and all other fields (visible or not) are copied too

This means that an assignment to a field of the copied value type will overwrite that value without affecting the original object. It’s like how a local variable behaves:

var x = 5;
var y = 2;
y = 3; // this does not change x

However, when the type is a reference type, the assignment second = first just copies the reference. The underlying object where the values are kept are the same for both. So changes to either object affect the other—because there is no “other”: it’s the same object that’s just being referenced by two separate variables.


To answer the follow-up questions from the comment:

How do I need to imagine it that an int variable is a value type and string is a reference type? I mean, so the int variable directly contains the number and the string variable is just like a pointer to a storage location of the string object?

Yes, that’s exactly it. The value of a reference is basically just a pointer to the memory where the object actually is, while the value of a value type is the whole object itself.

That’s why e.g. having a value type as the parameter to a method means that when the method is called, the whole value type object is copied into the stack for the execution of the method.

A type being a value type does not mean that its members will all be value types too though. For example, the actually stored value of a string member inside a value type is still a reference to the string object. Similarly, the actual memory for a reference type will contain the actual value for value types, and references to other memory locations for reference types.

If the parent type always matter more than the objects types, what can I use the type of int and string for?

There is nothing that matters more than another. An object being a value or a reference type has only implications on how that object is stored. For members of the object, this is evaluated completely separately.

Is it possible if you just have one class and a bunch of variables to set some of them just as a reference to another variable, for example int variables.

You cannot have a reference pointer for a value type, like you could have in C, no. But you can have references to fields which allow you to mutate the value of fields that are of a value type. For example:

class Test
{
    private int field;

    public void ShowExample()
    {
        // set the value
        field = 12;

        // call the method and pass *a reference* to the field
        // note the special syntax
        MutateField(ref field);

        // the field, which is a value type, was mutated because it was passed as a reference
        Console.WriteLine(field == 4);
    }

    private static void MutateField(ref int value)
    {
        value = 4;
    }
}
Thursday, August 26, 2021
 
Geovany
answered 4 Months ago
69

I was able to fix this issue by making browser sleep for few seconds after every change in URL.

Below is the code snippet:

67 it('should create new job listing', function () {
68        //Login As Admin To Access Vacancies Feature
69        loginAsManager();
.
.        //load manager's dashboard list page
.        dashboardPage = new DashboardPage();
.        dashboardPage.vacanciesTab.click();
.
.        //load vacancies list page
.        var vacanciesUrl = browser.baseUrl + '#/vacancies';
.        browser.sleep(2000);
.        expect(browser.getCurrentUrl()).toEqual(vacanciesUrl);
.        vacanciesPage = new VacanciesPage();
.        vacanciesPage.addVacancyButton.click();
.
.

I don't think this is a neat solution to this problem.

I will be happy to hear, if someone has a better solution to this.

Cheers Gaurav

Friday, October 15, 2021
 
Morrison Chang
answered 2 Months ago
94

Maybe a dependency error. I'll suggest you to use a better practice for such lib.

  1. Put the fullcalendar scripts into vendor/assets/javascripts

  2. Require this script manually in application.js before the tree and after jquery and any other lib it depends.

    /=require 'fullcalendar`
    

Add

Check the loaded Javascript files in header, if application.js is the last, you have dependency error. The lib must be there before calling it.

Thursday, November 11, 2021
 
Yuriy Faktorovich
answered 4 Weeks ago
46

Maybe you should:

one, two, three = e.split(":")

as e.split(":") is already an iterable with three values.

If you write

for (one, two, three) in something

Then something must be an iterable of iterable of three values, e.g. [[1, 2, 3], [4, 5, 6]], but not [1, 2, 3].

Monday, November 15, 2021
 
Silver Light
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share