New answers tagged

0

In the world of angularJS, it's not compulsory to declare a variable. Angular allows developer to code without it's declaration and never throws error for the same. this mean it is more intelligent so you don't need to go every time on the top of the screen to declare variable like other programming languages. Example: function hello(){ ...


0

I'll summarize your question as: Currently I am using using Jquery, JqueryUI menus, Zeno Rocha's clipboard.js, to achieve that (context menus with copy functions, etc) but I am not sure it is the best approach. The menu is dynamically generated. Should I consider the contextmenu of some recent browsers? And anser with: http://caniuse.com/#feat=menu ...


1

One of the problems with URLs embedded in JavaScript code has to do with minification tools that interpret any // in a string literal as a comment. This can clobber hundreds of lines of code. In one case, an innocent reference to a git repository clobbered 400 lines of code for my company. Perhaps your client is reacting to that. If so, you might try ...


1

If your team uses backticked strings frequently enough for its benefits (template expansion, multiline strings, different escaping, tagged templates), then you'll probably make fewer mistakes by sticking with that one syntax. That's because (1) you'll be more used to those escaping rules, (2) you won't have to (remember to) change quotes and escapes when ...


-1

let means "let variable equal". It's a declaration, in other words, an initialization and assignment. It exists in contrast to const which of course means "constant" -- which is the opposite of variable. Some other languages use a prefix to the actual object instead of the object when declaring it (e.g. def is a shorthand for "define function" -- ...


3

In general, something like this is a good idea. I'm not very experienced with JavaScript in particular, but have loads of experience testing other languages. And there I have found that exposing callback hooks like this is an extremely valuable technique to allow dependency injection without drastically changing the overall architecture – in fact, I recently ...


0

I wouldn't call it an anti-pattern, I'd just call it silly (in most cases). You are presumably using premises because you're dealing with some asynchronous work. That said, you should just assume that its status is pending until it says otherwise. And with that said, you will presumably also have a section of code that deals with the promise ...


1

The main problem I'm aware of with synchronous inspection is that it's completely unnecessary, unless your code relies on the promise resolving or rejecting within a certain number of ticks, which is exactly the sort of thing you should not rely on. For instance, although I know nothing about React, I'm pretty sure your example could be changed into ...


0

The purpose of a promise or future is to allow the promise to pursue calculations while you go and do something else ("in another thread"). Once you're done doing your other thing and come back to the promise to interrogate it for a value, the expectation is that you will then wait for the value (having completed "doing your other thing"). If you want to ...


1

Here's a good answer by the bluebird author about the speed issue with ES6 promises. Obviously, he has a bias, but he's also looked at the problem in quite some detail. Those issues that slow down ES6 promises are because of the way they are specified, and therefore are still present. I don't think it's drastic enough for speed to be your deciding factor, ...


1

Checking the file suffix seems easy, but ultimately pointless. A file named foo.bar might be a valid jpeg file. A file named reallyIsA.jpg might be a nasty virus. Remember when some corporate firewalls forbid you from emailing .exe or .zip files and everybody just changed the extensions? IMO, your API should specify "a file in one of these image formats: ...


1

You should provide an API with documentation. In your documentation you should document what unusal inputs are handled well, what incorrect inputs you will detect and how you will respond to them - and what incorrect inputs you will not detect. And your implementation should follow that documentation. Your API can be written with the attitude "calling it ...


0

In this example and in most other real world examples the overhead of validation will be negligible compared to the work your API is supposed to do. For starters you may want to create a class MediaFile that encapsulates the input of your API. All validation can then be done in the constructor of the media file class. MediaFile could be abstract and you ...


3

I'd argue no-ish. I'm not sure it's really the "module pattern" if you return anything other than a singleton. But, you can certainly use the same "pattern" to accomplish other things. The module protects the global scope from the internal variables used to build the return-value; but the return value can itself be a function -- including a constructor. To ...


0

You can return anything with the module pattern, it's just a way to avoid global pollution while instantiating a module. It's quite common to see it used to generate functions or constructors, which can then be used as often as needed: var module = (function (){ ...scoped stuff... return function () { ...do stuff with scoped stuff... }; ...


0

In my ES6 const is not about immutability post, I explain what const means exactly according to the spec. Based on those objective facts, here’s my personal preference: […] it makes sense to use let and const as follows in your ES6 code: use const by default only use let if rebinding (i.e. any form of reassignment) is needed (var shouldn’t ...


6

In general, a Web API is a programming interface that HTML applications can leverage for "external" resources. In practice, an "external resource" is in one of two categories: Web services: These are interfaces generally accessible over the web via HTTP(S) (e.g. REST, SOAP). They're developed by whoever builds or maintains the target server-side ...


2

I feel like profiling this would be overkill. Technically you're passing less on the second way but the performance hit/gain (if any at all) using either is negligible. At this point it's more of about maintainability. Normally you would attach the the event via JavaScript as well but if for some reason you can't and you will need the whole element anyway, ...


2

@MichaelT asked how to rewrite the C# example code for goto without using goto. Here is their code: using System; class Test { static void Main(string[] args) { string[,] table = { {"Red", "Blue", "Green"}, {"Monday", "Wednesday", "Friday"} }; foreach (string str in args) { int row, colm; for (row = ...


-5

Yes, it is a 'code smell'. This harks back to the days of spagettii code, gotos, sosubs and breaking out of loops to carry on with a procedure when ine of several conditions had been reached. These practices have almost universaly been replaced with OO style methods, while, foreach and inhertiance. So if you still have break in your code you should ask ...


27

Having a break out of a loop is no different than having that loop get refactored out to a function of its own and a return statement in a guard clause. while(condition) { if(test) { break; } doStuff; } vs doMuchStuff(); function doMuchStuff() { while(condition) { if(test) { return; } doStuff; } } Those are effectively the same. ...


0

This JavaScript snipped implements and show two particular points : a Closure: that is a special habitability of some languages to capture a variable into another context. the fact that in Javascript any variable is an object (a string, an integer, a function, a class, ...) This snipped finally provide a function noisy() that can run another function ...


2

Consider the line noisy(Boolean)(0); Boolean is a javascript function that takes an optional parameter. The first part of the line in question, noisy(Boolean)(0); , takes the Boolean function, and passes it into the noisy function as a parameter. The noisy function returns a (new) function that takes a parameter (return function(arg)). The second half of ...


6

Since it's not clear what part of this is confusing you, let's take this step by step. 1) Boolean is a function. In this case, it takes one argument, and returns either true or false depending on whether the argument was truthy or falsy. So if it helps, you could replace Boolean with function(x) { return !!x; } and get roughly the same behavior. 2) This ...


2

First keep in mind that this code is not changing/mutating the passed in function. Passing in the Boolean constructor is a bit odd and may lead to some confusion so lets remove it and break it down more for clarity. var noisyIsArray = noisy(Array.isArray); noisyIsArray([]); > calling with [] > called with [] - got true true noisyIsArray(2); > ...


2

You don't. That's the problem. When types are not declared on a method's arguments, and there isn't a comment on the code saying what it's expecting to receive, there are only two ways to figure out what it expects you to pass. Either examine the function itself and see what it's doing with the input, or look at documentation. (Or sample code, which is ...


1

I would argue that, in the case of AngularJS, utilizing callbacks is a code smell. I say this for the pure simple fact that AngularJS includes the notion of promises that you can leverage for the same use-case, and in fact look extremely similar in practice to the code you have already developed. Consider your test method: var testMethod = function() { ...


0

You can use macros in C/C++ to change identifiers as needed. For javascript the obvious candidate is var since it is only needed in javascript ... it can be redefined as necessary to any c type. c-header.h //constant integers #define var const int #include "int-consts.js" #undef var //constant strings #define var const char* #include "string-consts.js" ...


1

Dynamically typed or not, it's best to consult documentation and/or examples. Even if you know what type a parameter is, it's often quicker and safer to base your code off of an example, with more detailed documentation on hand, than it is to assume you know what you're doing based on the parameter type. Otherwise, you're just making assumptions and ...


7

This isn't a problem unique to JavaScript, or even just dynamic languages. For example, you might have the equivalent Java code: void init(Configuration conf) { ... } and then discover that Configuration is an final class with no public constructors and you're left searching around for how to obtain an instance of that class. Typically, looking at ...


-3

Trial amd error. Actually, I doubt there's a resource without a proper documentation in it. If you really can't find what goes where, then you could type the function's name without parentheses in the chrome live console, and then, if the functiom is not a native function, then it prints out the source of the function.:


1

Refer to the HTML-component in one single place: the selector. var exampleTable = $("#example"); function main() { exampleTable.DataTable(); } This way, if the name changes, or if the way you select the table changes, you only need to make the change in one single place. This also means you can split up your code into components: initialize a ...


4

As is often the case, it depends With a pure JS framework you push a lot of processing to the clients. That means your webserver for the client application will only be serving static files that can be cached. your clients have to be able to handle that processing and need some amount of computing power your clients need to fully support all JS-features ...


5

Neither Format Is REST Standard. REST does not define representation standards. REST does not specify the format of resources. REST is an "architectural style" not a standard. Fielding Dissertation - The Original Document Describing REST It is your application's decision whether to use a hierarchical representation or a flat representation, or to offer ...


0

Would this be an acceptable solution if the number of data members or fields of the class is large? export class State { private _dirty: boolean = false; private _data1: string = ''; private _data2: number = 0; constructor( d1: string, d2: number ) { this._data1 = d1; this._data2 = d2; } get dirty() { ...


1

Based on comments, here's a dirty-bit implementation that should work: class Dirtyable { private _isDirty:Boolean = false; get isDirty():Boolean { return _isDirty; } private _example:String; get example():String { return _example; } set example(val):String { if (val !== _example) _isDirty = true; ...


1

It sounds like you've discovered that you can use javascript closures to create classes. Doing this you can treat javascript as an object oriented language. But is this wise? Look at java 8. Java is desperately trying to be more functional. It was originally intended to be an object oriented language. Which you should lean towards, OO or functional, ...


1

The code you have there is about as far from functional as could possibly be. The entire function is built around a side-effecting loop which mutates a value. Here's the trivial functional implementation of a map operation: Array.prototype.map = function (fn) { const [first, ...rest] = this; return this.length === 0 ? [] : ...


1

I think it is a local scope, or an anonymous local scope, that is shield any local variables from leaking to the global scope. Yes, that's precisely what it is. And because JavaScript doesn't actually have block scopes, the only way you can implement it is with functions. Hence, IIFEs. To say that you are using a closure here is true, because the ...


1

Should I rely solely on mongoose's validation for input validation, or should I write my own before it even gets to mongoose? I think that kind of checks should be done by your service, between DB and HTTP modules, not inside DB. For output filtering, should I create a security / filtering module which accepts a Product and returns a ...


2

It depends upon your JavaScript code and your DOM (and in principle, of the JavaScript implementation in the browser; invite your users to use recent versions of browsers). You need to read more about garbage collection techniques (e.g. the GC handbook), see Mozilla SpiderMonkey's GC page. A value which is still indirectly reachable from global or local ...



Top 50 recent answers are included