difference between `new Object()` and object literal notation? [duplicate] - javascript

This question already has answers here:
What is the difference between `new Object()` and object literal notation?
(12 answers)
Closed 4 years ago.
can anyone tell me actually what it is (var abc={} <==this one is object or?) if that is object what different between var abc=new Object() and var abc={};
Another question is scanner scan =new Scanner(); is same concenpt with var abc= new Object(): ??

Objects can be defined by either of these two methods:
var abc = {};
var abc = new Object();
There is minimal difference between the two, however the first method is preferred by most.
If Scanner is of type Function then you instantiate it like so:
var scan = new Scanner();
The Scanner function might have been created like this:
function Scanner(total = 5){
this.scans = total;
}
You could use this function like this:
var scan = new Scanner();
console.log(scan); // Output: Object
console.log(scan.scans); // Output: 5
scan = new Scanner(100);
console.log(scan.scans); // Output: 100
scan.scans = 50;
console.log(scan.scans); // Output: 50
var scan2 = { scans: 5 };
console.log(scan2); // Output: Object
console.log(scan2.scans); // Output: 5

For an empty object, both var abc = {} and var abc = new Object() works but there are different approaches with different scenarios/requirements to choose the appropriate style.
You can read more at:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
Go to "Creating new objects"
For
var scanner = new Scanner();
Scanner must be a function.

Related

Cannot get update property from prototype [duplicate]

This question already has answers here:
javascript two variables with same name co-exist in same object?
(5 answers)
Closed 1 year ago.
In the following code I do not understand why the last console.log does not return 'kj18', although the pro-type has been changed before that line.
Could you please provide me an explanation?
var studentId = 'ab73';
function Student() {
this.studentId = 'b0e1';
}
console.clear()
console.log(new Student().studentId);
Student.prototype.studentId = 'kj18'
Student.prototype.classId = '5e'
console.log(new Student().classId)
console.log(new Student().studentId) // why is not kj18
Because the assignment in the function Student() gets executed when you create a new instance from Student and it all happens after this assignment: Student.prototype.studentId = 'kj18'
Adding a log to the "constructor" helps explain it.
var studentId = 'ab73';
function Student() {
this.studentId = 'b0e1';
console.log('assigned b0e1')
}
console.log(new Student().studentId);
Student.prototype.studentId = 'kj18'
console.log('assigned kj18')
Student.prototype.classId = '5e'
console.log(new Student().classId)
console.log(new Student().studentId) // why is not kj18

How to change a variable that is defined as to an array without having to define it twice [duplicate]

This question already has answers here:
Javascript create reference to an object property?
(4 answers)
Are there pointers in javascript?
(6 answers)
Closed 3 years ago.
so it's kinda hard to explain ( more that I don't know the terminology for it but that's beside the point) but basically:
var myArray = [1,2,3];
var wantedData = myArray[1];
console.log(wantedData);
myArray[1] = 4;
console.log(wantedData);
so here I changed the array from [1,2,3] to [1,4,3] but the value of wantedData still outputs as 2. Did I do something wrong or would I just have to define it again by doing
var myArray = [1,2,3];
var wantedData = myArray[1];
console.log(wantedData);
myArray[1] = 4;
wantedData = myArray[1];
console.log(wantedData);
wantedData is being assigned the actual value at myArray[1] not just a reference to it. So changes to the values in the array will not be reflected in the value wantedData as it would with C-style pointers.
wantedData is a primitive data type whereas myArray is reference type. So, when the reference changes, the primitive variable will not be affected from it.
In Javascript advanced types are passed by reference and primitive types by value. it means that if you do that:
let a = 5;
let b = a;
b = 10;
in this case, JS engine copies a's value and assigns to b. they have separated values in memory.
in case of advanced types ( advanced means not a single value or in other words, everything what is not a primitive type, is an object )
var a = {
name: 'Name'
};
var b = a;
b.name = "New value";
memory pointer for var a and var b are the same, because advanced(reference) types in Javascript are passed by reference. they are linked to the same spot in memory.
Other answers are correct, but if you want to obtain what you ask, you can use a thing like this:
var myArray = [1,2,3];
var wantedData = ()=> myArray[1]; // or function(){return myArray[1]}
console.log( wantedData() ); // 2
myArray[1] = 4;
console.log( wantedData() ); // 4

How can I make a static field in Javascript [duplicate]

This question already has answers here:
Static variables in JavaScript
(43 answers)
Closed 9 years ago.
I have an object in Javascript like this:
var Person = {name: "John", age:37}
I would like that the name will be statically accessible every time I make a new instance of the class while the age will always be new.
So for example:
var per1 = new Person();
Person.name = "Anton";
Person.age = 11;
per1.Name //Anton
per2.Name //11
var per2 = new Person();
per1.age //Anton
per2.age //37 ????
Is there a way to make it so that it works that way?
In order to make a propery static in javascript you can use prototype:
Person.prototype.name = "Anton"
UPDATED:
You might want to use it this way:
var Person = function(age){
this.age=age;
}
Person.prototype.name = "Anton"
var per1 = new Person(12);
console.log(per1);

how to null an object which is an array's element in javascript [duplicate]

This question already has answers here:
Deleting Objects in JavaScript
(14 answers)
Closed 8 years ago.
Suppose i have an Array of objects in javascript :
var obj0 = new Object();
var obj1 = new Object();
var obj2 = new Object();
var obj3= new Object();
var array = new Array(obj0,obj1,obj2,obj3);
if i write :
array[1] = null;
this will give me [obj0,null,obj2,obj3] what was nulled is the array case not the object itself; the obj1 won't really be nulled in the memory.
How to null an object by accessing it via the array ?
You just need to remove all references to the object, including the obj1 reference. Then the garbage collector will take care of the rest.
However, if your obj1 variable is a local variable (as it appears to be in your code snippet), you can just leave the reference as is. When the enclosing method returns, the local variable will be cleaned up, and subsequently, the nulled object as well.
I found a good article on Smashing Magazine that looks relevant to your question.
It’s not possible to force garbage collection in JavaScript. You
wouldn’t want to do this, because the garbage collection process is
controlled by the runtime, and it generally knows best when things
should be cleaned up.
http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/
i think "splice()" will help:
http://www.w3schools.com/jsref/jsref_splice.asp
var obj0 = new Object();
var obj1 = new Object();
var obj2 = new Object();
var obj3= new Object();
var array = new Array(obj0,obj1,obj2,obj3);
array.splice(1); // kill index 1
The result: [obj0,obj2,obj3].
What if you tried to write it this way?
var array = new Array();
array[0] = new Object();
array[1] = new Object();
array[2] = new Object();
array[3]= new Object();
array[1] = null;
As commented Kpower, writting :
var array = [{}, {}, {}, {}];
or
var array = new Array(new Object(),
new Object(),
new Object(),
new Object());
and when nulling any array cell like :
array[1] = null;
this will effectively remove the reference for the object so it will be garbage collected.

Difference between "{}" and "new Object()" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
creating objects - new object or object literal notation?
What exactly is the difference between the following:
var myData = new Object();
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";
and
var myData = {};
myData["name"] = "ATOzTOA";
myData["site"] = "atoztoa";
Update
What I got is this...
var myData = {
"name" : "ATOzTOA",
"site" : "atoztoa",
};
is a shortcut to
var myData = new Object({
"name" : "ATOzTOA",
"site" : "atoztoa",
});
Am I right?
There is no difference (technically). {} is just a shortcut for new Object().
However, if you assign an object literal, you may directly form a new object with multiple properties.
var myData = {
name: 'ATOzTOA',
size: 'atoztoa'
};
..which might feel more convenient. Also, it reduces the access on the object and is ultimately faster. But that is about microoptimizations. More important is that its a lot less to type.
Nothing. {} just a short hand for new Object()
Its same logic as your full name is 'Mark Zuckerberg' and people call you ' Hi Mark'
No difference in my view , Both are initializing the object. nothing else , it is a shortcut.

Categories