ReferenceError: ProductStore is not defined - javascript

I'm trying to migrate some old javascript / backbone code over to our new system and I'm running into the following error.
ReferenceError: ProductStore is not defined
ProductStore.Options = {},
ReferenceError: ProductStore is not defined
ProductStore.type= "board";
My JS file looks like this.
ProductStore.Options= {},
function() {
"use strict", ProductStore.Options.Product = Backbone.Model.extend({
//do something
})
}(),
function() {
"use strict", ProductStore.Options.ProductView = Backbone.View.extend({
//do something
})
}()
There is no other js files, so I'm wondering what I'm doing wrong?

Error says it all, you can't say:
ProductStore.options = {}
unless you have already declared ProductStore (and defined it to be an object).
e.g.
var ProductStore = {};

You have to create a ProductStore JS object first:
var ProductStore = {};

You have to change your , with ;
var ProductStore = {};

Not sure but best practice is to declare object is to make it fail safe. Someone told me the same in one of my project.
var ProductStore = ProductStore || {};

Related

How to write a function to disable console.log and can be required in other react file?

Below is my static.js file:
var Helper = {
console.log: function(){
},
Login: function(){
var name;
var password;
//rest of the code
}
}
module.exports = Helper;
And below is my test.js file:
var Helper = require('./static.js');
console.log("Test");
And I got some error from this line console.log: function(){} in static.js file.
What I want is nothing will show on terminal even I console.log('Test') because I write function(){} for console.log.
Is anything I did wrong?
Just overwrite console.log function in your script:
console.log = function() {};
Overwrite other log function too:
window.console.log = window.console.debug = window.console.info = window.console.error = function () {
return false;
}
I just figured out how to fix this problem.
I rewrite the function like below..
DisableConsole: function(DEBUG){
if(!DEBUG){
if(!window.console) window.console = {};
var methods = ["log", "debug", "warn", "info"];
for(var i=0;i<methods.length;i++){
console[methods[i]] = function(){};
}
}
}
and require this static.js file in my top component which mean every component under this main component will also include this static.js.
and call this function in the very beginning.
AppHelpers.DisableConsole(false);
You have this error because you're trying to create an object with a key console.log, but it is a syntax violation when using object literal syntax, because . dot is a special symbol.
Even if it worked, you wouldn't achieve what you want, since console.log is a global function and you are working with just a custom created object.
What you actually want to do is to silence the global function like this console.log = function() {};. Beware, however, that you won't be able to restore the old behaviour if you didn't save the original function: var oldConsoleLog = console.log.
There is also a module for it.

Javascript Strategy Design Pattern issue

I am following this gist for a strategy design pattern.
https://gist.github.com/Integralist/5736427
I implemented this a few months ago and the chrome extension did not throw any errors when I implemented it but now its throwing a
"Uncaught TypeError: this.strategy is not a function"
var MessageHandling = function(strategy) {
this.strategy = strategy;
};
MessageHandling.prototype.greet = function() {
return this.strategy();
};
its strange because the functions that rely on this code still run but some other code that does not rely on it is limited.
Any ideas on how to fix it?
This is mainly about the object you create from MessageHandling, if you pass the right function while creating the object it should always work.
var MessageHandling = function(strategy) {
this.strategy = strategy;
};
MessageHandling.prototype.greet = function() {
return this.strategy();
};
var m = new MessageHandling(function(){console.log('hello');});
m.greet();
The above code will always work but if you instantiate the MessageHandling with passing a parameter which is not a function or not passing a parameter at all then it will complain that this.strategy is not a function. So you need to make sure you pass the right function to MessageHandling while creating its object.
You need to post your full code here in order for someone to figure out the problem. But from the exception text, it looks like you are passing an undefined strategy or a "variable that is not a function" to the constructor. The following sample will give the same "Uncaught TypeError: this.strategy is not a function exception:
// This is the Greeter constructor.
var Greeter = function(strategy) {
this.strategy = strategy;
};
// Greeter provides a greet function that is going to
// greet people using the Strategy passed to the constructor.
Greeter.prototype.greet = function() {
return this.strategy();
};
// Here are a couple of Strategies to use with our Greeter.
var politeGreetingStrategy = function() {
console.log("Hello.");
};
var friendlyGreetingStrategy = function() {
console.log("Hey!");
};
var boredGreetingStrategy = function() {
console.log("sup.");
};
var undefinedStrategy; //Not a function variable
// Let's use these strategies!
var politeGreeter = new Greeter(politeGreetingStrategy);
var friendlyGreeter = new Greeter(friendlyGreetingStrategy);
var boredGreeter = new Greeter(boredGreetingStrategy);
var wrongGreeter = new Greeter(undefinedStrategy); //No such strategy defined
politeGreeter.greet(); //=> Hello.
friendlyGreeter.greet(); //=> Hey!
boredGreeter.greet(); //=> sup.
wrongGreeter.greet(); //-> uncaught type

rquirejs - getting issue from global space

In my app, I created a global space for my app saying socialApp. even before i call the initialize method, I am declaring the socialApp in the window like this:
define([
'jquery',
'underscore',
'backbone',
'marionette',
'scripts/appRoutes',
'scripts/app/appModel',
'scripts/app/layout/appLayout'],
function ($,_,Backbone,Marionette,Routes,appModel,appLayout) {
"use strict";
socialApp = window.socialApp || {}; // i am declared the socialApp here.
socialApp = new Backbone.Marionette.Application();
socialApp.addInitializer(function(options) {
socialApp.NewRoute = new Routes;
socialApp.GenModel = new appModel({router:socialApp.NewRoute}); //i am getting error saying "Uncaught ReferenceError: socialApp is not defined" - why?
socialApp.Layout = new appLayout();
socialApp.Layout.render();
Backbone.history.start();
});
It is not happening always, but some instance i am getting this error. how to resolve this?
in the appModel i declared like this :
define(['jquery','underscore','backbone','marionette'],
function ($,_,Backbone,Marionette) {
"use strict";
socialApp = window.socialApp || {}; //exactly i am getting error from here on refresh some times..
socialApp.Model = Backbone.Model.extend({
initialize:function(currentRoute){
this.router = currentRoute.router;
}
});
return socialApp.Model;
});
Where i do the mistake? any one figure out me please?
You can't declare global variable within an AMD module like that, socialApp is a local variable within the module itself. So either declare an AMD module for your socialApp variable like this:
File: socialApp.js
define(['marionette'], function(Marionette) {
var socialApp = {};
return socialApp;
});
And require socialApp from everywhere you use it.
File: appModel.js
define(['jquery','underscore','backbone','marionette', 'socialApp'],
function ($,_,Backbone,Marionette, socialApp) {
socialApp.Model = Backbone.Model.extend({
initialize:function(currentRoute){
this.router = currentRoute.router;
}
});
return socialApp.Model;
});
Or, if you insist on global variables; declare socialApp within window explicitly.
replace socialApp = window.socialApp || {}; with window.socialApp = window.socialApp || {};.

Express.JS not recognizing required js file's functions

Even though, I've imported the JS file that includes the function that I will use, Node.JS says that it is undefined.
require('./game_core.js');
Users/dasdasd/Developer/optionalassignment/games.js:28
thegame.gamecore = new game_core( thegame );
^
ReferenceError: game_core is not defined
Do you have any idea what's wrong? Game_core includes the function:
var game_core = function(game_instance){....};
Add to the end of game_core.js:
module.exports = {
game_core : game_core
}
to games.js:
var game_core = require('./game_core').game_core(game_istance);
Requiring a module in Node doesn't add its contents to the global scope. Every module is wrapped in its own scope, so you have to export public names:
// game_core.js
module.exports = function (game_instance){...};
Then keep a reference in your main script to the exported object:
var game_core = require('./game_core.js');
...
thegame.gamecore = new game_core( thegame );
You can read more about it in the docs: http://nodejs.org/api/modules.html#modules_modules
An alternative approach:
if( 'undefined' != typeof global ) {
module.exports = global.game_core = game_core;
}

Javascript inheritance misbehaviour

I have some js code here link deleted
If you open your js console and you'll run this code snipet
var r = new TempPopupForm("xxx");
r.create();
an error will appear
TypeError: this.init is not a function
this error is saying there is no init method implemented on this object.
But that's not true.As you can see in the code below the init method is declared.
TempPopupForm.prototype = new PopupForm();
TempPopupForm.prototype.constructor = TempPopupForm;
TempPopupForm.superclass = PopupForm.prototype;
function TempPopupForm(name) {
this.init(name);
}
TempPopupForm.prototype.init = function(name) {
TempPopupForm.superclass.init.call(this, name);
};
I guess something is wrong with the inheritance definition,but I can not figure out what it is.
BTW There are some third party dependencies.
EDIT
I was following this article and where the funcs are ordered like I have. The order actually works on the other classes, but not on this one.
http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm
You need to re-order your functions and instantiation. Since your constructor is using one of its own prototyped method to init, they both need to be above the block where you instantiate the object. JavaScript hoists top-level functions.
Try this -
function TempPopupForm(name) {
this.init(name);
}
TempPopupForm.prototype = new PopupForm();
TempPopupForm.prototype.constructor = TempPopupForm;
TempPopupForm.superclass = PopupForm.prototype;
TempPopupForm.prototype.init = function(name) {
TempPopupForm.superclass.init.call(this, name);
};
var r = new TempPopupForm("xxx");
r.create();

Categories

Resources