About js objects [duplicate] - javascript

This question already has answers here:
Constructors in JavaScript objects
(19 answers)
Closed 8 years ago.
my English is not so good, but i try to explain my self clear.
I just started learn JS objects and stumbled upon the problem that i can't understand.
I got a simple object like
var cars = {
doors: 4,
wheels: 4
}
and when i trying to create an object like this:
var Opel = new car()
I got an error Uncaught TypeError: object is not a function
And when i do it like this :
Opel = Object.create(cars)
all going fine.
And when i writing a an Object like this :
function cars() {}
a method to declare the object with new, work correctly.
I can't understand what the difference between thous two type of writing the objects.
Thanks for advice.

You didn't understand prototyping correct.
To define a class you create a simple function, like:
function Car(){
this.doors = 4; //For instance, not really necessary
}
You can set properties on this in the function.
Then you define a prototype, every object of class "Car" will have all this properties (and "methods"):
Car.prototype = {
doors: 4, //we don't need to set this again if we already did in the constructor, but I'll leave if it anyway
wheels: 4
}
Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript for more.

Related

how object.constructor() works? [duplicate]

This question already has answers here:
Javascript constructor return values [duplicate]
(2 answers)
Closed 5 years ago.
Recently I have attended one interview, Interviewer asked one interesting question have a look
function MyClass(){
this.a = 10;
return 20; // Interesting part
}
var obj1 = new MyClass();
console.log(obj1.a); // 10 works as expected.
console.log(obj1.constructor()); // 20 later I found this
how will you access return value(20) from obj1?
I found the answer after looking proto of the obj1.
obj1.constructor() Works as expected
Please help me to understand this.
See mdn:
The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)
The 20 goes nowhere and cannot be accessed. It isn't an object, so the instance of MyCass is returned instead.

Javascript Constructor vs Object Literal Confusion [duplicate]

This question already has answers here:
Should I be using object literals or constructor functions?
(12 answers)
Closed 5 years ago.
I recently came across constructors for creating multiple objects with the same properties and methods.
Method 1:
function Person(name) {
this.name = name;
}
And then you can instantiate it like so:
var bob = new Person("bob");
What i would like to know is there a difference between using the standard constructor method and just returning an object inside a function like so:
Method 2:
function Person(name) {
var obj = {
name: name,
};
return obj;
}
I am still able to create multiple objects using the same function. I am just slightly confused on why you would use 'Method 1'? Is it because i can extend the first method using the prototype property? Am i able to extend and create more methods using 'Method 2'?
Am i right in thinking this is why you use constructors because they are more flexible and can be added to and modified whereas the Object literal type inside a function cannot? Also what other benefits does it bring?
Sorry if this is a silly question.
Thanks, any information would be nice
Consider:
function Person(name) { /* either of your approaches */}
Person.prototype.getName = function() {
return this.name;
}
Method 1 will work as expected, method 2 will not, if you call:
new Person('Bob').getName ()

Javascript Functions and Objects Confusion [duplicate]

This question already has answers here:
Function and Object Javascript
(2 answers)
Closed 5 years ago.
In Javascript, we have two fundamental building blocks called functions and objects. But I'm a bit confused about the phrase functions are special type of objects. Anyways, in Javascript:
We create functions like this:
function foo(){}
Now the above declared function also behaves like an object as below:
foo.staticMethod = function(){}
Ok. I understand it.
Now similarly we create objects like this:
var obj = new Object() // Not using object literal here
That means, we need a function constructor Object to make even an empty object.
But Functions are also objects. How????
So my simple question is, if Object is used to create any new object, then how it can be an object itself as it accepts a property Object.prototype or I should say how a function can be an object ?
function Object(){
return {};
}

How to create the instance of the class by its string name? I tried to use `window` for these purposes also. [duplicate]

This question already has answers here:
How do I make JavaScript Object using a variable String to define the class name?
(10 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 5 years ago.
JS6, Browser
How to create the instance of the class by its string name? For example I have a string Foo and I need to create an instance of Foo class. I don't want to write such construction:
let className = getClassName(); // returns 'Foo', `Stuff` or other
...
let item = null;
if(`Foo` == className){
item = new Foo();
}
else if(`Stuff` == className){
item = new Stuff();
}
UPD
I try to get access to my class constructor through the window object but I have the problem: it returns undefined. But my class exists and browser knows it:
What you are trying to do is what you should do, in my opinion. This is a standard way of instantiating objects when the type of object is known at runtime, and is called the Factory Method Pattern.
You should avoid duplicating this everywhere in your code by using a factory object that will encapsulate the object creation. This factory object will have a method that will take the type name as its parameter.
Please use google: Instantiate a JavaScript Object Using a String to Define the Class Name
var myObject = window[classNameString];
P.S. I copy pasted your title of question and added javascript in front.

What is the difference between these two ways of changing a functions prototype in JavaScript? [duplicate]

This question already has answers here:
Is it ok to assign the JavaScript prototype object instead of just its properties?
(4 answers)
Closed 6 years ago.
In the following code a constructor function, an object instance, and two examples of changing a functions prototype. What is the difference between these two methods? As far as I can work out the second method will update the object instance even if it is after the declaration of that instance, whereas the other will not. Is this the only difference?
function Foo(name, color) {
this.name = name;
this.color = color;
}
var bar = new Foo('name', 'color');
First method:
Foo.prototype = {age: 6};
Second method:
Foo.prototype.age = 4;`
Foo.prototype = {age: 6};
First method is going to override constructor and ____proto____ link ,if you were using inheritance not a good approach becuase you will destroy the proto links and prototype chain
Its better to use
Foo.prototype.age = 4;`
In this way you are just only adding a property all the prototype chains will remail intact

Categories