Creating Objects in Javascript with Constructor - javascript

I am trying this code
var mark = new Employee;
mark.name = 'Doe, Mark';
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);
But the console won't show anything when i try to print it. It shows only when i use new Object. What is the problem?

You need to define Employee as a constructor function:
function Employee() {}
var mark = new Employee();
mark.name = 'Doe, Mark';
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);
This will also allow you to set properties with the constructor like:
function Employee(name){
this.name = name
}
var mark = new Employee('Doe, Mark');
mark.dept = 'admin';
mark.projects = ['navigator'];
console.log(mark);

Related

How to add a method to an array object that was made by a construction function

trying to solve the following problem.
I have a construction function like:
function Person (name){
this.firstName = name;
}
than I make some objects:
var myFather = new Person("John");
var myMother = new Person("Mary");
var myChild = new Person ("Sonny");
and finally attach them together:
var family = [myFather, myMother, myChild];
Now I would like to attach a method driver() to 'family', that will use the 'firstName' variable from the constructor function to choose, who is going to drive
Use another class Family and add a setDriver method to it:
function Person (name){
this.firstName = name
}
function Family (members){
this.members = members;
this.driver = members[0];
this.setDriver = function(name){
this.driver = this.members.filter(member => member.firstName == name)[0]
}
}
var myFather = new Person("John");
var myMother = new Person("Mary");
var myChild = new Person ("Sonny");
var family = new Family([myFather, myMother, myChild]);
console.log(family.driver)
family.setDriver("Sonny")
console.log(family.driver)
// At first you should remove comma from you Person function, and use semicolon(;)
// Just create function
function driver(firstName = this.firstName) {
console.log(`Drive is ${firstName}`)
}
driver.call(this, family[0].firstName)
// Result Drive is John
// or you can directly call driver function as method of any family object like
driver.call(family[0])
// Above code will produce same result, please try this

Getting the object variable name for the new object

I have an object constructor such as:
function myObjConstr () {
console.log(object_name);
}
I want this results:
var name1 = new myObjConstr(); //print in console log "name1"
var nameff = new myObjConstr(); //print in console log "nameff"
You would need to pass the object name to the constructor:
function myObjConstr (obj_name) {
this.object_name = obj_name;
console.log(this.object_name);
}
var name1 = new myObjConstr("name1");
var nameff = new myObjConstr("nameff");
you can make a function constructor myObjConstr(), thenafter you can make new myObjConstr().
1) function constructor
function myObjConstr (objName) {
this.objName = objName;
}
2) make object of type myObjConstr
var name1 = new myObjConstr("name1");
var name2 = new myObjConstr("name2");
3) if you want to print the value,
console.log(name1.objName)
You can't pass the variable name to the constructor. Instead you can convert an array of names of variables to array of objects
let names = [
'name1',
'nameff'
]
let objects = names.map(name => myObjConstr(name));
function myObjConstr(name){
this.name = name;
console.log(this.name);
}

Accessing a private method in javascript

The following is my javascript code for accessing a private method. But it is not working. I receive a TypeError: string is not a function message. Can anyone please help me?
Here is my code:
function Boy(firstName,lastName) {
this.fisrtName = firstName;
this.lastName = lastName ;
var ladyLove = "Angelina";
var returnLove = function() {
return ladyLove;
};
this.sayLoud = function(){
return returnLove();
};
}
var achilles = new Boy("Bradley","Pitt");
var sayNow = achilles.sayLoud();
console.log(sayNow());
sayLoud() returns Angelina - which is a String, not a function.
You probably just want to go for:
console.log(sayNow);
Instead of using a string as a function, you should use console.log(sayNow);
Explained:
var achilles = new Boy("Bradley","Pitt"); // Will create a new object
var sayNow = achilles.sayLoud(); // call sayLoud(), return string
console.log(sayNow); // output the string
try this
var achilles = new Boy("Bradley","Pitt");
var sayNow = achilles.sayLoud;
console.log(sayNow());

Hide static variable from other functions

I'm having trouble getting my function to increment user id on the creation of every new object, but also preventing other functions from changing it.
function User(u_name) {
this.user_name = u_name;
this.user_id = User.next_user_id++;
}
User.next_user_id = 1;
user1 = new User('john');
user2 = new User('jane');
if I move next_user_id into function User as var next_user_id then it gets reset on every new object, which is not what I want.
How can I increment user_id for every new object but prevent other functions from changing it?
you should try looking up closures...
var User = (function()
{
var next_user_id = 1;
return function(u_name)
{
this.user_name = u_name;
this.user_id = next_user_id++;
}
})()
user1 = new User('john');
user2 = new User('jane');
You need to attach the variable to the function's prototype.
Try:
User.prototype.next_user_id = 1;
If you want to control access to (C# like) internal only, you need to turn it into a property.
var $Mynamespace$User$Count = 0;
function $Mynamespace$User$IncrementUser() {
$Mynamespace$User$Count++;
};
var User = function(name){
this.name = name;
$Mynamespace$User$IncrementUser();
};
User.prototype = {
GetUserCount: function() { return $Mynamespace$User$Count; }
};
var jane = new User("jane");
var john = new User("john");
alert(User.prototype.GetUserCount());
alert(jane.GetUserCount());

JavaScript - Meaning of: var DndUpload = function (inputElem){};

Can I kindly ask for explanation:
What does the code below represent? Does it create a DndUpload Ojbect? Or, does it create a DndUpload() function? What I miss is the statement new normally present during JavaScript objects creation. Can I kindly ask for some explanation, as I am confused.
var DndUpload = function (inputElem)
{
this.input = inputElem;
this.dropZone = null;
this.isDragging = false;
this.init();
};
As far as I know this is the way to create object in Javascript:
var myObject = new function()
{
};
If you have any link with explanation, that would help. Thank you.
It's a worse way of writing this:
function DndUpload(inputElem)
{
this.input = inputElem;
this.dropZone = null;
this.isDragging = false;
this.init();
}
which is a function declaration. It does not create an instance of DndUpload. Technically, it does create an object – its name is DndUpload and it is an instance of Function. To create an instance of this "class:"
var instance = new DndUpload(document.getElementById('someInputId'));
var myObject = new function()
{
};
Defines an anonymous constructor function and then instantiates a new object using the anonymous constructor function. It could have been replaced with var myObject = {}.
var DndUpload = function (inputElem)
{
this.input = inputElem;
this.dropZone = null;
this.isDragging = false;
this.init();
};
Defines a constructor function (technically an anonymous constructor function assigned to a variable). You can then create objects of this "class" by invoking the constructor function with new:
var dndUploadObject = new DnDUpload(),
anotherUploadObject = new DnDUpload(); //2 unique DnDUpload objects
the code you have essentially creates a constructor for a "class" it's more or less a blueprint for an object.
It then puts that constructor into a variable called DndUpload
So you can now make an object with
var myObject = new DndUpload(input elem)

Categories

Resources