This question already has answers here:
Why does this JavaScript code print "undefined" on the console?
(1 answer)
Why does the JS console return an extra undefined? [duplicate]
(1 answer)
Closed 8 years ago.
I have code following like this:
function Person() {
this.name = '123';
this.age = 123;
}
Person.prototype.load = function() {
console.log(this.name + " test ");
}
var p1 = new Person();
console.log(p1.load());
the console output two news. One is 123 test, the other is undefined.I wonder where is undefined comes from?
The load functions returns nothing, that is it returns undefined. And you log this undefined here :
console.log(p1.load());
You probably just want
p1.load();
The return value of console.log is ALWAYS undefined. It prints to the console, but it doesn't actually return anything itself.
var tmp = 5;
console.log(tmp); // prints 5, returns undefined.
tmp; // Returns 5
Also why are you printing out the result of a function that already prints out the information you want?
hi you do not need to take too much tension about that undefined. Your code is correct and it will work fine in any js file.
That undefined comes as you make instance of person class.
http://jsfiddle.net/5azsp5r9/
Here you go mister:
function Person() {
this.name = '123';
this.age = 123;
}
Person.prototype.load = function() {
//Load started for John Doe
console.log("Load started for "+ this.name );
return "Load ended";
}
var p1 = new Person();
p1.name = "John Doe";
//Load ended
console.log(p1.load());;
Related
function Person(name,age){
this.name=name;
this.age=age;
}
var person1 = new Person("name1",4)
var person2 = new Person("name2",6)
function Animal(name,size){
this.name=name;
this.size=size;
}
var animal1=new Animal("name1","small")
var animal2 = new Animal("name2","big")
Person.prototype.sayName=function(){
console.log("Hello "+[name])
}
Animal.prototype.sayName=function(){
console.log("Hello "+[name])
}
animal1.sayName();
I just learned Javascript and I started playing around with some code. When I run this code, the console prints out undefined. I believe the console should print : "Hello animal1". What is wrong with it?
In javascript, when you say new, first thing that would happen is that an empty object would be created. Upon the creation of this object, the animal function is executed and the current execution context would be referring to newly created empty object. So when you say this.name = name and this.size = size, the this keyword would be referring to the newly created object. So you need to always reference the property with this, while accessing it as shown in the below snippet:
function Animal(name,size){
this.name=name;
this.size=size;
}
var animal1=new Animal("name1","small")
Animal.prototype.sayName=function(){
console.log("Hello "+this.name)
}
animal1.sayName();
Hope this answers your question
you have to specify the this keyword to refer to the current instance.
Animal.prototype.sayName = function(){
console.log("Hello "+ this.name)
}
console.log("Hello " + [name]) should be console.log("Hello " + this.name)
I tried it in my console, now it outputs Hello name1.
Suppose I have two functions test1 and test2!
test1 = function(name){
console.log('Hi, ', name);
};
test2 = function(name) {
var text = 'Hello ' + name;
var say = function() { console.log(text); }
return say;
}
Now I call the function and save them to vars,
var t1 = test1('John');
var t2 = test2('John');
What is exactly happening here?
Lets understand this:
In below code you are defining a function called test1 which will print Hi, John if you use test1('John').
test1 = function(name){
console.log('Hi, ', name);
};
Now lets understand below code, below you are defining a function called test2 which:
test2 = function(name) {
var text = 'Hello ' + name;
var say = function() { console.log(text); }
return say;
}
var text = 'Hello ' + name; //define a variable called text
var say = function() { console.log(text); } //defininga a function called say which will log the data in variable text in the console.
Then you are returning say function.
OUTPUT:
Input: => var t1 = test1('John');
Output: => Hi, John //It's quite straight forward.
Input: => var t2 = test2('John');
Output: => undefined //Here you are re-assigning function returned from test2 which was say to t2. Hence if you now type t2 then you wil get function () { console.log(text); } as output which is similar to say.
I hope you understood, sorry for poor english.
Your function test1 returns void, since there is no return statement
This function prints Hi John to the console
Your function test2 returns the function say() to the console and stores it in t2. This makes t2 a function. If you pay attention to the syntax in test2 that's one of the two standard ways of defining a function in javascript. if you call
t2();
you will get the similar output to test1('John');:
Hello John
your first function executes a code and don`t return any thing then t1 will be undefined. but second function returns a function then t2 contains a function that never called so it will not log to console
On calling, test2 returns 'say' which is a pointer to a function, not the value. That is why it will not output anything.
To call function 'say' you need to do test2('john')().
In the second pair of parenthesis, we pass in the arguments to the function inside.
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Closed 5 years ago.
Given following code:
var User = function(name) {
this.name = name;
};
User.prototype.sayHello = function() {
console.log('Hi my name is ' + this.name + '.');
};
var user1 = new User('Bob');
user1.sayHello();
What i learned so far is that the this keyword when used in function statements points at the global object, and when used in methods, at the object it lexically sits in.
I also know that the new keyword creates an empty object and calls the constructor and letting it point to that new object.
But what I dont understand is, since user1 doesn't own the sayHello function,
it goes up the prototype chain.
But how does the function in the prototype know where to refer with this.name?
The output in the console is: Hi my name is Bob.
Because its created from User object if you do an console.log on user1.name you will get Bob as output next sayHello is called with reference to user1 so this.name resolves to Bob
var User = function(name) {
this.name = name;
};
User.prototype.sayHello = function() {
console.log('Hi my name is ' + this.name + '.');
};
var user1 = new User('Bob');
console.log(user1.name)
user1.sayHello();
This is a code the works fine in Codecademy which is where it's from. However, when I try the same code in the browser, it keeps returning undefined.
<script>
function Cat(name, breed) {
this.name = name;
this.breed = breed;
}
Cat.prototype.meow = function() {
console.log('Meow!');
};
var cheshire = new Cat("Cheshire Cat", "British Shorthair");
var gary = new Cat("Gary", "Domestic Shorthair");
alert(console.log(cheshire.meow));
alert(console.log(gary.meow));
</script>
You're passing the result of console.log() to alert but it doesn't return anything so you're passing undefined to alert.
Either use only alert or only console log, don't pass one to the other.
Your meow function already logs to the console, so doing it again is pointless. Most likely what you want is this:
cheshire.meow();
gary.meow();
Note that since meow is a function, you probably want to actually call it and not just print the function itself.
I am learning js and am confused on one thing. I am just copying/pasting the code example from mdn...
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayGender = function() {
alert(this.gender);
};
var person1 = new Person('Male');
var genderTeller = person1.sayGender;
person1.sayGender(); // alerts 'Male'
genderTeller(); // alerts undefined
alert(genderTeller === person1.sayGender); // alerts true
alert(genderTeller === Person.prototype.sayGender); // alerts true
in the example above genderTeller will return undefined but after slightly modifying it in the second example:
function Person(gender) {
this.gender = gender;
}
Person.prototype.sayGender = function() {
alert(this.gender);
};
var person1 = new Person('Male');
var genderTeller = person1.sayGender();
genderTeller();
the genderTeller function actually works but also returns 'typeError: genderTeller is not a function.'
Why is genderTeller not a function in the second example when it's clearly assigned to a method?
In the first example, you are assigning a function to a variable and executing it. The this in that case is window, since you are just calling the function and not calling it on anything.
In the second case, you are invoking the function and assigning its result to genderTeller. Since the function doesn't return anything, the variable is undefined.
Look at this line:
var genderTeller = person1.sayGender();
You are assigning the value returned by calling the function since you are executing it, not the function itself.
Since the function doesn't actually return a value, it's not a function (or anything at all, really), which is why you are getting that error.