jasmine, javascript - javascript

i just started with learning Javascript and Jasmine and stumbled about the following error Message when i run the test: "ReferenceError: greet is not defined"
// greetSpec.js
describe('greeter', function() {
return it('should greet with message and name', function() {
var result;
result = greet('Hello', 'John Doe');
return expect(result).toBe('Hello, John Doe!');
});
});
// greet.js
var greet;
greet = function(message, person) {
var greeting;
return greeting = "" + message + ", " + person + "!";
};

The function is defined before the declaration of greet. Think of the file being scanned from top-to-bottom. By the time it gets to the describe call, the variable greet does not exist, hence the error. Try putting the greet function definition above the call.
Turns out, as Lennier put it, that there was "a conflict between some of the coffeescript generated files." Glad I could help though.

Related

Console prints out undefined. What is wrong with my code?

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.

Changing function scope

I am trying to test this module:
function fa(){
return "function A"
}
function fb(){
return "function B + " + fa()
}
module.exports = {fa, fb}
I test it this way:
var myModule = require("./myModule")
var sinon = require("sinon")
var util = require('util');
sinon.stub(myModule,"fa").returns("stub")
console.log(myModule.fb()) //expect to display "function B + stub"
It is actually displaying "function B + function A"
If i replace the method body of fb by return "function B + " + this.fa() (notice the addition of this.) then it works as expected.
Is there a way to mock a function which haven't been called with this?
There is no handy way to do that. You can create an object and use it as a reference, so you call that function as a "method"
What happens is that you are calling that function "as function", the function itself is different from the one that you are exporting.
This read can clarify more about what I said.
Understanding 'this' keyword

Node module is undefined when required

I'm getting a very odd error when I try to require a node module. To illustrate the problem here is the code I am trying to require:
module.exports = (function(){
this.say = function(message) {
console.log(message);
}
})();
Now when I require this module I get 'Cannot read property 'say' of undefined when I try to use it as follows:
var person = require('./Person.js')
person.say('Hello World!');
And yet, if I define the module as follows it works fine ...
module.exports = {
say : function(message) {
console.log(message);
}
};
I even tried this notation that also worked ...
module.exports = new Person();
function Person(){
this.say = function(message) {
console.log(message);
}
};
Does anyone have any idea why the first notation doesn't work properly?
The reason is your first notation doesn't return anything to export.
module.exports = (function(){
this.say = function(message) {
console.log(message);
}
return this;
})();
I guess this should solve your problem.
module.exports is an object that will be returned upon require calls for that file.
So assume you want write a library that does some logic, some is private, other is public.
and you want to expose only your public function to other people to invoke,
what your file may look like is:
// Some require calls you maybe using in your lib
function privateFunc(s) {
console.log(s);
}
function publicFunc(s) {
privateFunc("new log: " + s);
}
module.exports = {
log: publicFunc
}
You later will use it as follows:
var logger= require('myLib');
logger.log("my log");
This will output:
new log: my log
Thats maybe deeper than what you actually wanted, but it is meant to understand the flow of things

Why the console output undefined? [duplicate]

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

Why should I assign a function to a variable in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?
I am attempting to understand the "best practices" of javascript.
This code is from jqfundementals.com
// create a function that will greet a person,
// and assign the function to the `greet` variable
var greet = function( person, message ) {
var greeting = 'Hello, ' + person + '!';
log( greeting + ' ' + message );
};
greet( 'Jory', 'Welcome to JavaScript' );
greet( 'Rebecca', 'Thanks for joining us' );
Why should I assign the function to the greet variable?
My first impulse would be to write it like this:
function greet ( person, message ) {
var greeting = 'Hello, ' + person + '!';
log( greeting + ' ' + message );
};
What are the differences between these two implementations?
There aren't any differences between those snippets, except hoisting which allows you to call the former function in the lines before the definition. But this is just a simplistic example to get you warmed up. In reality, people don't assign these functions to variables but pass them directly to other functions. Or they otherwise use them in expression contexts. Or they dynamically decide which function to to store. Or anything else really.
There is no real difference but the var form enables you to declare-before-use incase you have recursive functions.
Simple example:
var func1, func2;
func1 = function (count) {
count = count - 2;
if (count > 0) {
func2(count);
}
}
func2 = function (count) {
func1(count + 1);
}
func1(10);
Although
function func1 (count) {
count = count - 2;
if (count > 0) {
func2(count);
}
}
function func2 (count) {
func1(count + 1);
}
func1(10);
is perfectly acceptable too. The interpreter will replace it with the former because of variable hoisting.

Categories