Monday, 22 April 2019

remove a property from a JavaScript object?


remove a property from a JavaScript object?

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

What does “use strict” do in JavaScript?


What does “use strict” do in JavaScript?

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.
And:

Strict mode helps out in a couple ways:

It catches some common coding bloopers, throwing exceptions.
It prevents, or throws errors, when relatively "unsafe" actions are taken (such as gaining access to the global object).
It disables features that are confusing or poorly thought out.

Also note you can apply "strict mode" to the whole file... Or you can use it only for a specific function

// Non-strict code...

(function(){
"use strict";

// Define your library strictly...
})();

// Non-strict code...

check whether a string contains a substring in JavaScript?


check whether a string contains a substring in JavaScript?

var string = "foo",
substring = "oo";
string.includes(substring)

includes doesn’t have IE support, though. In an ES5 or older environment, String.prototype.indexOf, which returns −1 when it doesn’t find the substring, can be used instead:

var string = "foo",
substring = "oo";

string.indexOf(substring) !== -1

remove a particular element from an array in JavaScript?


remove a particular element from an array in JavaScript?

var array = [2, 5, 9];
console.log(array)
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}

// array = [2, 9]
console.log(array);

How JavaScript closures work?


How JavaScript closures work?

function sayHello(name) {
var text = 'Hello ' + name;
var say = function() { console.log(text); }
say();
} sayHello('lisa');

can I know which radio button is selected via jQuery?


can I know which radio button is selected via jQuery?

$('#myForm input').on('change', function() {
alert($('input[name=radioName]:checked', '#myForm').val());
});

how use transparent background using CSS?


use transparent background using CSS?

background-color:rgba(255,0,0,0.5);

remove a property from a JavaScript object?

remove a property from a JavaScript object? delete myObject.regex; // or, delete myObject['regex']; // or, var prop = ...