This question already has answers here:
How to determine equality for two JavaScript objects?
(82 answers)
Closed 8 years ago.
I am confused why the following does not evaluate to true.
How do I compare whether two objects are the same?
var x = new Object();
var y = {};
x == y // false
function Person(name) {
this.name = name;
}
var p1 = new Person("Chris");
var p2 = new Person("Chris");
p1 == p2 // false
Without going into how a JS engine works you can understand it by just thinking of it like objects are in the real world. If x is a ball and y is a chair, they aren't equal just because they are both objects. And if you know two people named Chris they aren't the same person they just have the same name.
Related
This question already has answers here:
Understanding the difference between Object.create() and new SomeFunction()
(11 answers)
What is the 'new' keyword in JavaScript?
(17 answers)
Closed last year.
Are the following two constructions identical, or are there any differences between them?
let x = new Item(v1, v2, ...)
And:
let x = Object.create(Item.prototype)
x.constructor(v1, v2, ...)
The code I was writing to see which one to use is the following (though for academic purposes):
'use strict';
function Meal(type) {
this.type = type;
};
Meal.isMeal = function(obj) {console.log(obj instanceof Meal)};
Meal.prototype.eat = function() {console.log(`${this.type} has been eaten!`)};
// let breakfast = new Meal('Breakfast');
let breakfast = Object.create(Meal.prototype);
breakfast.constructor('Breakfast')
Meal.isMeal(breakfast);
breakfast.eat();
This question already has answers here:
How does JavaScript .prototype work?
(26 answers)
Closed 8 years ago.
Given:
var x = function () {
};
x.prototype = { abc: 25 };
Can someone explain to me what this means. Could this be done all inside a function without the .prototype?
var x = function () {
// something here ?
};
Prototypes are how the class model works in JavaScript - you've created a class x that has a property abc which defaults to 25:
var obj = new x();
alert(obj.abc); // 25
The function x is the class constructor, it is called when a new instance of that class is created and can initialize it. And that means of course that you can just set the abc property there:
var x = function()
{
this.abc = 25;
};
var obj = new x();
alert(obj.abc); // 25
This is supposedly the less efficient approach however:
You have to manipulate each object created rather than setting the property on the prototype once and forever.
The property is stored on each object and consumes memory each time, as opposed to being stored once on the prototype.
ECMAScript Harmony has a nicer syntax for defining classes and prototypes, however this one isn't implemented in any browser yet:
class x {
constructor() {
...
}
public abc = 25;
}
This is equivalent to your code defining the prototype, merely grouping related operations a little better.
This question already has answers here:
dynamic keys for object literals in Javascript [duplicate]
(8 answers)
in javascript is it possible to construct an object literal with expressions evaluating to strings for property names? [duplicate]
(2 answers)
variable in javascript statement
(2 answers)
Closed 8 years ago.
I'm bored and messing with my console and I came up with the following code:
I was trying something like:
x = 16;
y = 26;
f = { x+y:"String!"};
expecting something or somehow to do:
Object {1626: "String!"}
Or at least
Object {42: "String!"}
I ended up with
x = 16;
y = 26;
eval("f = { "+x+y+":\"String!\"}");
Which returned as expected:
Object {1626: "String!"}
I've always been told to avoid eval() and never even think about using it due to something about security. Is there a way to use variables in declaring the property (Sorry if I don't know what it's called exactly)
x = 16;
y = 26;
f = {};
f[x+''+y] = "String!"; // For f[1626]
or
f[x+y] = "String!" // For f[42]
I made this exact same mistake when I started learning JavaScript: JavaScript set object key by variable
You first must make the object:
var f = {};
Then you can use variables to dynamically create keys:
var x = 16, y = 26;
f[x+y] = "Integer";
f[x.toString() + y.toString()] = "String";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Javascript type of custom object
I have a question regarding JavaScript instances.
Let us consider the following code:
function Box(col)
{
var color = col;
this.getColor = function()
{
return color;
};
}
var blueBox=new Box("blue");
console.log(blueBox.getColor())
var greenBox=new Box("green");
console.log(greenBox.getColor())
console.log(typeof(blueBox))
console.log(typeof(greenBox))
Now, when we check the last two statements, the browser prints type as object
How do I check If they are created from same constructor Box?
You can use instanceof like:
var blueBox=new Box("blue");
if (blueBox instanceof Box){
//yay 4 boxes!
}
In case you want to check two elements you can also compare their constructors:
var blueBox = new Box("blue");
var greenBox = new Box("green");
if (blueBox.constructor === greenBox.constructor){
//yay 4 same constructors
}
use instanceof
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/instanceof
Your custom object Box is an object as far as javascript is considered, however it can be an instance of a Box which is of type object.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Pass Variables Through Reference
How can you do something like this in javascript?
In PHP you can put a & in a function in front of the parameter to write directly back to the variable.. How can you do something like this in javascript?
In JavaScript, values are passed to functions by value. Objects, however, are passed by reference.
Passing values:
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 objects:
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
Source: http://snook.ca/archives/javascript/javascript_pass
If you pass an object into a function it is always treated as passing a reference to it.
To have a similar effect for primitive values, you might wrap them in an object before like
var intVal = { val: 0 };
That way you can pass it to a function and still modify the actual variable.