Constructor Doesn't Initialize Online Javascript - javascript

I just need to reposition elements for app. The starts up locally but not deployed online. I am using "use strict" also.
var winHeight=$(window).height();
var winWidth=$(window).width();
var Layout = function(){
this.half=480;
this.targetX=960;
this.targetY=527;
this.resultX=0;
this.resultY=0;
this.compute= function(xx,yy){
var reX = xx-this.targetX;
var reY = yy-this.targetY;
var teX = reX+this.targetX;
var teY = reY+this.targetY;
this.resultX=teX/this.targetX;
this.resultY=teY/this.targetY
};
};
var layout = new Layout();
layout.compute(winWidth,winHeight);

You just need to say: function ClassName()
function Layout() {
...
};

Related

Possible failed dependency injection using require throws 'is not a function' error

I've looked through a few questions related to this error, and most of them seem to be a misunderstanding of what the keyword this means. I don't think I'm having that problem here. Mine might be some sort of circular dependency problem that I cannot articulate well enough to figure it out on my own.
I've tried to distill my problem into three files presented below.
something.js
var A = require('../lib/a');
var Something = function (type) {
this.type = type;
};
Something.prototype.setTemplate = function (template) {
this.template = template;
};
Something.prototype.applyTemplate = function () {
var templateResult = this.template.calculate();
};
var factory = {};
factory.createSomething = function(type) {
return new Something(type);
};
factory.createA = function (input) {
return A.Make(input);
};
module.exports = factory;
a.js
var S = require('../prof/something');
var _ = require('underscore');
var A = function (input) {
this.input = input;
};
A.prototype.calculate = function () {
var calculation = 0;
var _s = S.createSomething('hello world');
// do calculation using input
return calculation;
};
var factory = {};
factory.Make = function (input) {
var a = new A(input);
return a;
};
module.exports = factory;
a_test.js
describe('Unit: A Test', function() {
var S = require('../prof/something');
it('test 1', function() {
var a = S.createA({
//input
});
var s = S.createSomething('type1');
s.setTemplate(a);
s.applyTemplate(); // error
});
});
The error gets thrown from the top level in a_test.js on the line with the comment //error. At the lowest level, the 'is not a function ' error is thrown in a.js at the S.createSomething(type) method. It says that S.createSomething() is not a function.
I've put a breakpoint in at that line and tried to call functions from the underscore library, but it gives the same error. So it seems that the require statements inside a.js are not throwing errors, but none of the injected objects can be used to call functions from. The a_test.js file is being run with the karma library.
Am I violating some javascript paradigm by referencing back and forth between A and S? How can I do this properly?
Edit: I've done some further testing. It doesn't actually matter if the test file looks like this:
describe('Unit: A Test', function() {
var S = require('../prof/something');
it('test 1', function() {
var a = S.createA({
//input
});
a.calculate(); // error
});
});
An error is still thrown at the line indicated above.
The files in the question reference each other. This is called cyclic dependencies. The solution is to move the var S = require('../prof/something'); statement into the calculate function like so:
a.js
// move the line from here
var _ = require('underscore');
var A = function (input) {
this.input = input;
};
A.prototype.calculate = function () {
var S = require('../prof/something'); // to here
var calculation = 0;
var _s = S.createSomething('hello world');
// do calculation using input
return calculation;
};
var factory = {};
factory.Make = function (input) {
var a = new A(input);
return a;
};
module.exports = factory;

How to set/redefine variable in external js file

How can I re-define variable in external js file 'js/sample.js'?
The main idea was not touch core file and be possible to pass a new value (to var COUNT_DEFAULT) on js load from my new module.
core js example:
(function(_, $) {
var COUNT_DEFAULT = 2;
...
Not sure if I understood you well, but you can do it like that.
var module = (function (myVal) {
var COUNT_DEFAULT = myVal || 2;
return {
val: COUNT_DEFAULT
};
});
var defaultVal = module();
var customVal = module(45);
console.log(defaultVal);
console.log(customVal);

JavaScript Objects Error using dot notation

$(document.body).on("click",'.sub-unfollow', function(){
var unfollow_tag = {element:"",un:"",type:"",text:""};
var unfollow_tag.element = $(this).parents("li");
var unfollow_tag.un = $(this).parents("li").attr("data-un");
var unfollow_tag.type = $(this).parents("li").attr("data-type");
var unfollow_tag.text = $(this).parents("li").text();
alert(unfollow_tag.text);
});
Getting an error with this seemingly basic object setup. Any ideas?
You should remove var after once declaring unfollow_tag, it redeclares it instead of trying to access a property.
$(document).on("click",'.sub-unfollow', function(){
var unfollow_tag = {};
unfollow_tag.element = $(this).parents("li");
unfollow_tag.un = $(this).parents("li").attr("data-un");
unfollow_tag.type = $(this).parents("li").attr("data-type");
unfollow_tag.text = $(this).parents("li").text();
alert(unfollow_tag.text);
});

JavaScript "Prompt" command not working when inserting code

I'm trying to make a project that searches through a block of text, then pushes certain values to the properties of an object, but whenever I put a variable inside of the ingamePrices object at near the bottom of this block of text,
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
};
document.write(testPrompt);
so that it's like this
var testPrompt = prompt("Let's figure out how this works");
var rawUSDValue = 0.125;
function item(craftGamePrice, craftMarketPrice, uncraftGamePrice, uncraftMarketPrice, strangeGamePrice, strangeMarketPrice, genuineGamePrice, genuineMarketPrice, vintageGamePrice, vintageMarketPrice, unusualGamePrice, unusualMarketPrice, hauntedGamePrice, hauntedMarketPrice, collectorGamePrice, collectorMarketPrice )
{
this.craftGamePrice = craftGamePrice,
this.craftMarketPrice = craftMarketPrice,
this.uncraftGamePrice = uncraftGamePrice,
this.uncraftMarketPrice = uncraftMarketPrice,
this.strangeGamePrice = strangeGamePrice,
this.strangeMarketPrice = strangeMarketPrice,
this.genuineGamePrice = genuineGamePrice,
this.genuineMarketPrice = genuineMarketPrice,
this.vintageGamePrice = vintageGamePrice,
this.vintageMarketPrice = vintageMarketPrice,
this.unusualGamePrice = unusualGamePrice,
this.unusualMarketPrice = unusualMarketPrice,
this.hauntedGamePrice = hauntedGamePrice,
this.hauntedMarketPrice = hauntedMarketPrice,
this.collectorGamePrice = collectorGamePrice,
this.collectorMarketPrice = collectorMarketPrice
}
var ingamePrices =
{
var testVariable = "sampleString";
};
document.write(testPrompt);
it causes the "prompt" command to stop working. Does anyone know why, or how to fix it?
var ingamePrices =
{
var testVariable = "sampleString";
};
This might be an attempt at one of two things: an object literal, or block syntax which you imagine will contain testVariable. Object literals contain keys and values, they don't contain arbitrary expressions or variable definitions. As an object literal this should be
var ingamePrices =
{
testVariable: "sampleString"
};
Or possibly, if you really did want a testVariable as context for some of the contents of this object, then:
var testVariable = "sampleString",
ingamePrices =
{
blah: [testVariable, "a use of testVariable"]
};
If you were looking for block syntax, and lexical variables, then JavaScript doesn't have them. It only has global and function variables. Which means cases like this become a self-executing function, purely to provide scope:
var ingamePrices = (function() {
var testVariable = "sampleString";
...
return { blah: testVariable };
})()

listing variables inside a function in javascript

function show_alert(){
var month = oMonthList.value;
var day = oDayField.value;
var gametype = oGameTypeList.value;
var gamenum = oGameNumberField.value;
var gamename = oGameNameField.value;
var modname = oModNameField.value;
var phase = oPhaseList.value;
var phasenum = oPhaseNumberField.value;
var pagenum = oNameNumberField.value;
var repname = oReplacementNameField.value;
var modlink = oModLinkField.value;
alert(phase);
}
Why does this not show the alert when the function is called, but removing all variables except the one in question (var phase) does? I'm guessing it's something to do with syntax, but I cannot pin down the issue.
Did you make sure that your javascript code doesn't throw any exception? If some object is undeclared or undefined, the code may be aborted early thus alert() is not not executed.

Categories