Usage of Object.create vs new [duplicate] - javascript

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();

Related

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

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.

JavaScript object and datatype [duplicate]

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.

What does it mean to add a prototype to a function? [duplicate]

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.

javascript custom object typeof name [duplicate]

This question already has answers here:
Get name of object or class
(8 answers)
Closed 8 years ago.
I would like to know how I can get the name of my custom object in javascript?
var calendarDay = function (date) {
//someCode
}
var test = new calendarDay(new Date());
console.log(typeof test); //Object
However, if I do this in Chrome:
console.log(test);
Chrome shows the exact name of my customObject. Can I do this in javascript?
You're looking for test.constructor.name. You should note however that this will only work with named functions, not function expressions wherein a variable is assigned an anonymous function.
// named function, not function expression
function calendarDay (date) {
//someCode
}
var test = new calendarDay(new Date());
console.log(test.constructor.name); //"calendarDay"
EDIT:
Big surprise, as #Bergi pointed out, it's not fully supported by IE. Check out the full compatibility table on the MDN Docs
Not sure if this could help you.
give a name for your custom function obj.
var calendarDay = function (date) {
this.name = 'calendarDay';
}
var test = new calendarDay(new Date());
console.log(test.name);

checking the instances If they are constructed from same Constructor in javascript [duplicate]

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.

Categories