Surfing With Code

Tricks for developers about Java, HTML5, CSS, JavaScript, XML, jSON, deployment, design …

Dynamically access object property using variable — octubre 16, 2017

Dynamically access object property using variable

When working with JavaScript, it’s easy to update objects property referring to its property name, but sometimes this require big blocks of code or complex code.

But if we want to optimize our code, we can access dynamically to its properties. This way we will reduce our code!

So, to sum up, we have two ways of accessing object properties:

  • Dot notation
  • Bracket notation

Source: MDN Web Docs


Dot notation

Captura de pantalla 2017-10-16 a las 10.33.35

Bracket notation

Captura de pantalla 2017-10-16 a las 10.33.53

Example

 const something = { bar: "Foobar!" }; const something = { bar: "Foobar!" };
// dot notation
something.bar = "Foobar 2";

// bracket notation
something["bar"] = "Foobar 3";

 

Anuncios
What is the difference between a URI, a URL and a URN? — octubre 14, 2017

What is the difference between a URI, a URL and a URN?

Common confusion right? Let’s see:

Source: StackOverflow

URI — Uniform Resource Identifier

URIs are a standard for identifying documents using a short string of numbers, letters, and symbols. They are defined by RFC 3986 – Uniform Resource Identifier (URI): Generic Syntax. URLs, URNs, and URCs are all types of URI.

URL — Uniform Resource Locator

Contains information about how to fetch a resource from its location. For example:

URLs always start with a protocol (http) and usually contain information such as the network host name (example.com) and often a document path (/foo/mypage.html). URLs may have query parameters and fragment identifiers.

URN — Uniform Resource Name

Identifies a resource by a unique and persistent name, but doesn’t necessarily tell you how to locate it on the internet. It usually starts with the prefix urn: For example:

  • urn:isbn:0451450523 to identify a book by its ISBN number.
  • urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66 a globally unique identifier
  • urn:publishing:book – An XML namespace that identifies the document as a type of book.

URNs can identify ideas and concepts. They are not restricted to identifying documents. When a URN does represent a document, it can be translated into a URL by a “resolver”. The document can then be downloaded from the URL.

URC — Uniform Resource Citation

Points to meta data about a document rather than to the document itself. An example of a URC is one that points to the HTML source code of a page like: view-source:http://example.com/

Data URI

Rather than locating it on the internet, or naming it, data can be placed directly into a URI. An example would be data:,Hello%20World.

URI/URL Venn Diagram

Generating random number in JavaScript in a specific range — octubre 13, 2017

Generating random number in JavaScript in a specific range

There are millions of times where while we are coding we need to generate a random number. The most of the times we need it as an Integer, to get some value (position) from an array.

Here you are how to generate a random number! For more info about how does the method works, visit the SO link! Enjoy!

Source: StackOverflow

@AuthorIonuț G. Stan



/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
 return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}

Understanding scopes prefixes in AngularJS custom Directives — abril 6, 2017

Understanding scopes prefixes in AngularJS custom Directives

‘@’ – Text binding / one-way binding. For passing strings. These strings support {{}} expressions for interpolated values
‘=’ – Direct model binding / two-way binding. The model in parent scope is linked to the model in the directive’s isolated scope.
‘&’ – Behavior binding / Method binding. For passing a method into your directive’s scope so that it can be called within your directive.

JavaScript: Escaping Special Characters — abril 3, 2017

JavaScript: Escaping Special Characters

Every programming language has it’s special characters – characters that mean something special such as identifying a variable, the end of a line or a break in some data. JavaScript is no different, so it provides a number of functions that encode and decode special characters.

  • The JavaScript escape function replaces most punctuation symbols with the equivalent hex-codes, but was found to be inadequate when it came to UNICODE character encoding and has been superseded by the encodeURI function.
  • The encodeURIComponent function is an extension of encodeURI, the difference being that it also escapes the following characters: , / ? : @ & = + $
  • The htmlentities function escapes characters which have special meaning inside HTML by inserting HTML entities in their place (eg. & in place of &). See our article on ASCII Character Codes for more details.

All functions have a complementary ‘decode’ function that pretty much does the opposite.

Example:

The apostrophe character: ‘

escape: \’

htmlEntitie: '

JavaScript: How to remove properties from Objects — marzo 20, 2017
[Quick trick] Remove or empty a column from a table in SQL — febrero 15, 2017
[Quick trick] How to View Which Postgres Version is Running — febrero 6, 2017
Groovy: the Elvis Operator ?: — agosto 25, 2016

Groovy: the Elvis Operator ?:

Groovy is good at shortening normal Java statements. The Elvis operator is to shorten the ternary operator. If we have a sensible default when the value is null or false (following Groovy truth rules) we can use the Elvis operator. And why is it called the Elvis operator? Turn your head to the left and you will know.


def sampleText

// Normal ternary operator.
def ternaryOutput = (sampleText != null) ? sampleText : 'Hello Groovy!'

// The Elvis operator in action. We must read: 'If sampleText is not null assign
// sampleText to elvisOuput, otherwise assign 'Viva Las Vegas!' to elvisOutput.
def elvisOutput = sampleText ?: 'Viva Las Vegas!'
<div class="line number0 index0 alt1 break"></div>

Via: MRHAKI blog

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04 — julio 6, 2016

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04

When you use Intellij IDEA 13 on Ubuntu 14.04 and from time to time the keyboard completely (yep, completely, it’s not about shortcuts) locks in IDEA. You still can do anything you want in IDEA with your mouse, also you can use the keyboard everywhere else except IDEA. And it can be fixed only by restarting IDEA so far… Howewer, you have a better solution:


ibus-daemon -rd

Enjoy!