jshint complaining about not defined? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does jshint warn that gConfiguration is not defined? I have it defined before the start of the func. I also tried placing it inside. I know I can declare it as a global variable in the configuration but I don't see what this is wrong? requirejs uses a similar pattern at the top of its declaration.
/*jshint strict:true, undef:true */
/*global Backbone: true, $: true */
var gConfiguraton;
(function (global) {
'use strict';
var MYAPP = global.MYAPP = {};
MYAPP.version = '0.0.1';
// Defaults
MYAPP.settings = {
// Authorization
authorizationKey: null,
authorizationTokenType: 'Bearer',
authorizationTimestamp: null,
// Proxy
proxyUrl: null
};
// Depend on jQuery and Backbone
if (typeof $ !== 'undefined' && typeof Backbone !== 'undefined') {
// Look for global configuration provided by user and merge their settings
if (typeof gConfiguration !== 'undefined') {
$.extend(MYAPP.settings, gConfiguration);
}
MYAPP.Events = $.extend({}, Backbone.Events);
}
} (this));

Because you've misspelled it in the var statement (missing i near the end). You have
var gConfiguraton;
you meant
var gConfiguration;
// ^--- i here

Just a simple typo: var gConfiguraton; needs to change to var gConfiguration;.

Related

Constructor in Strict Mode [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I've been searching a lot for an answer to this REALLY simple question, but I cannot find it:
" How to create constructors in strict mode ? "
fetch(`https://restcountries.com/v3.1/name/${country_name}`, )
.then(response => {
if(! response.ok)
throw new MyError(response.statusText, response.status)
[...]
.catch(err => {
renderNotFound(err.message, err.code)
[...]
class MyError {
constructor(message, code) {
this.message = message
this.code = code
}}
I know the .this keyword resolves to undefined in strict node. So how can we work around it to create constructors?
Thanks in advance
EDIT: fixed a missing } in the class MyError
How to create constructors in strict mode?
When using class syntax, constructor functions are always in strict mode, because the body of a class is always strict. (Most new kinds of scope defined in ES2015+ are strict by default.)
I know the .this keyword resolves to undefined in strict node. So how can we work around it to create constructors?
No workaround required. this is set to undefined when you call a function without doing anything to set this (for example: example()). When you use new, this is set to an instance of the thing being created before your constructor code is called (in a base constructor) or when you call super(/*...*/) (in a subclass constructor). (And when you do something else to set this (like example.method() or method.call(example)), this is set just like it is in loose mode.) Example:
class MyError {
constructor(message, code) {
this.message = message
this.code = code
}
}
const x = new MyError("Ooops!", 42);
console.log(x.message); // Ooops!
console.log(x.code); // 42
Just to underscore the point that class bodies are in strict mode by default:
class MyError {
// Code here is strict, even though the script
// context around it isn't
static example() {
console.log(this === undefined);
}
}
const loose = {
// Code here is loose like the script context around it
example() {
console.log(this === undefined);
}
};
let f;
f = MyError.example;
f(); // true
f = loose.example;
f(); // false
In a comment you've suggested that code doesn't work in strict mode. It should, assuming you have a } on your class. It does in this fiddle (can't do fetch in stack snippets, sadly):
"use strict";
class MyError {
constructor(message, code) {
this.message = message
this.code = code
}
}
fetch(`/this-will-404`, )
.then(response => {
if(! response.ok)
throw new MyError(response.statusText, response.status)
})
.catch(err => {
renderNotFound(err.message, err.code);
});
function renderNotFound(message, code) {
console.log(`Not found: ${message} ${code}`);
}

How to migrate javascript file into typescript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need some help/suggestion on how can I able to migrate javascript file into typescript.
I am very early beginner on typescript.
I have gone through many tuorials as well https://www.typescriptlang.org/docs/handbook/classes.html but not able to do so .
Any help/suggestion would be very helpful.
//eaxmple code
(function() {
"use strict";
var CONSTANTS = {
PROTOCOL_SECURE : "https://",
PROTOCOL : "http://"
};
var constantNew = window.constantNew = function() {};
constantNew.prototype.init = function(params) {
this.lobby = params.lobby;
this.bSecure = !!params.bSecure;
};
constantNew.prototype.getPMAInfo = function() {
var url = (this.bSecure? CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+CONSTANTS.URL_GETPMAINFO_PATH;
return Promise.resolve({baseUrl:url})
.then(callDemeter) // just a call to demeter to get PMA Info
.then(decoratePmaData) // a small decorator
.catch(checkGeneralFailure("getPMAInfo"));// final error handler
};
constantNew.prototype.hangoutAuthenticate = function(args) {
var baseUrl = (this.bSecure? DEMETER_CONSTANTS.PROTOCOL_SECURE:DEMETER_CONSTANTS.PROTOCOL)
+this.lobby
+DEMETER_CONSTANTS.URL_HANGOUT_AUTHENTICATE;
return Promise.resolve()
.then(requestChallengeFn(baseUrl)) // request challenge (higher order function to get the workflow function)
.then(authenticatePartialApplication(baseUrl, args.account, args.password, args.bValidateByConfCode)) // try first first auth round or fail
.then(decorateHangoutAuthResult) // decorate result to return
.catch(checkGeneralFailure("hangoutAuthenticate")); // final error handler
function decorateHangoutAuthResult(authResult) {
return {
strWebroom: authResult.webroom[0].webroomcode,
strWebroomResource: authResult.webroom[0].resource,
strDemeterAuthToken: authResult.AUTHTOKEN,
strAudioConfCode: authResult.webroom[0].audioconferencecode
};
}
};
}());
There's no direct way of doing it unfortunately (i.e., via a script or some utility). Typescript provides the following guide to get you running there: https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
That being the case, it might be better to start over. There's several options such as the Typescript Node Starter project in Github: https://github.com/microsoft/TypeScript-Node-Starter

Testing Error on node.js function with Mocha [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
With Node.js installed and testing with mocha,
I have two files, numbers.js and test.js in the same directory
Following the top answer: What is the purpose of Node.js module.exports and how do you use it?
numbers.js
var myFunc = function myFunc() {
return 10;
};
exports.myFunc = myFunc;
test.js
var assert = require('assert');
var numbers = require('./numbers');
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc;
assert.equal(result, 10);
});
});
But when running $ mocha it returns the error:
AssertionError: [Function: myFunc] == 10
What am I doing wrong?
You need to call your myFunc function. Add the parens
describe('numbers', function() {
it('first function returns 10', function() {
var result = numbers.myFunc(); <<<<<<
assert.equal(result, 10);
});
});
First of all, Inspect your numbers module is properly required or not.
var numbers = require('../numbers'); // Check path of file numbers.js
If yes, write :
var result = numbers.myFunc(); // your module exports 'myFunc' property not 'myfunc'
assert.equal(result, 10); // your function return '10' instead of '1'

Is that possible to call module factory from other javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have two javascript files separately and I want to retrieve one from another. So, I created Module factory as below;
var module = angular.module('test',[]);
module.factory('$result', function() {
var data = {something:"somewhere"};
return data;
});
and I want to called from other file. For now, I settled as below;
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($test) {
console.log(test);
});
Which part of me doing wrong ?? please help me and I'm newbie in angular
It should be like that ..
var module = angular.module('test',[]);
module.factory('Result', function() {
var data = {something:"somewhere"};
return data;
});
You can call like that.
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function(Result) {
console.log(Result);
});
Hope it might be work.
As the comments suggested, you inject your dependencies as parameters in your controller function:
var module = angular.module('myApp', ['test']);
module.controller('NearestController', function($result) {
console.log(test);
});

Revealing module implicity calling an internal function - is this a "smell" [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm trying to improve my javascript and have been using the revealing module pattern to good effect but I noticed that code in the module not in a function or the return is executed on creation have utilised this, such as:
var MyModule = function ($) {
setUp();
function setUp() {
// execute code to be run when module created
}
function doStuff() {
}
return {
doStuff: doStuff
}
}
I can see this is creating a form of constructor for the function which relies on it being called by var myModule = new MyModule(jQuery).
Any feedback on the rights or wrongs of this appreciated.

Categories

Resources