Tutorial by Examples: c

In JavaScript, functions may be anonymously defined using the "arrow" (=>) syntax, which is sometimes referred to as a lambda expression due to Common Lisp similarities. The simplest form of an arrow function has its arguments on the left side of => and the return value on the right...
Start by defining a Foo function that we'll use as a constructor. function Foo (){} By editing Foo.prototype, we can define properties and methods that will be shared by all instances of Foo. Foo.prototype.bar = function() { return 'I am bar'; }; We can then create an instance using the ...
Defining an Anonymous Function When a function is defined, you often give it a name and then invoke it using that name, like so: foo(); function foo(){ // ... } When you define a function this way, the Javascript runtime stores your function in memory and then creates a reference to th...
echo and print are language constructs, not functions. This means that they don't require parentheses around the argument like a function does (although one can always add parentheses around almost any PHP expression and thus echo("test") won't do any harm either). They output the string r...
Unlike in languages like Python, static properties of the constructor function are not inherited to instances. Instances only inherit from their prototype, which inherits from the parent type's prototype. Static properties are never inherited. function Foo() {}; Foo.style = 'bold'; var foo = ne...
5 Object.keys(obj) returns an array of a given object's keys. var obj = { a: "hello", b: "this is", c: "javascript!" }; var keys = Object.keys(obj); console.log(keys); // ["a", "b", "c"]
A list comprehension creates a new list by applying an expression to each element of an iterable. The most basic form is: [ <expression> for <element> in <iterable> ] There's also an optional 'if' condition: [ <expression> for <element> in <iterable> if <c...
A dictionary comprehension is similar to a list comprehension except that it produces a dictionary object instead of a list. A basic example: Python 2.x2.7 {x: x * x for x in (1, 2, 3, 4)} # Out: {1: 1, 2: 4, 3: 9, 4: 16} which is just another way of writing: dict((x, x * x) for x in (1, 2...
The fundamental part of most classes is its constructor, which sets up each instance's initial state and handles any parameters that were passed when calling new. It's defined in a class block as though you're defining a method named constructor, though it's actually handled as a special case. cla...
Set comprehension is similar to list and dictionary comprehension, but it produces a set, which is an unordered collection of unique elements. Python 2.x2.7 # A set containing every value in range(5): {x for x in range(5)} # Out: {0, 1, 2, 3, 4} # A set of even numbers between 1 and 10: {x f...
Django is a web development framework based on Python. Django 1.11 (the latest stable release) requires Python 2.7, 3.4, 3.5 or 3.6 to be installed. Assuming pip is available, installation is as simple as running the following command. Keep in mind, omitting the version as shown below will install t...
The map function is the simplest one among Python built-ins used for functional programming. map() applies a specified function to each element in an iterable: names = ['Fred', 'Wilma', 'Barney'] Python 3.x3.0 map(len, names) # map in Python 3.x is a class; its instances are iterable # Out: &...
To filter discards elements of a sequence based on some criteria: names = ['Fred', 'Wilma', 'Barney'] def long_name(name): return len(name) > 5 Python 2.x2.0 filter(long_name, names) # Out: ['Barney'] [name for name in names if len(name) > 5] # equivalent list comprehension #...
Variables can be accessed via dynamic variable names. The name of a variable can be stored in another variable, allowing it to be accessed dynamically. Such variables are known as variable variables. To turn a variable into a variable variable, you put an extra $ put in front of your variable. $va...
The subtraction operator (-) subtracts numbers. var a = 9; var b = 3; var c = a - b; c will now be 6 If a string or boolean is provided in place of a number value, it gets converted to a number before the difference is calculated (0 for false, 1 for true): "5" - 1; // 4 7 - ...
The multiplication operator (*) perform arithmetic multiplication on numbers (literals or variables). console.log( 3 * 5); // 15 console.log(-3 * 5); // -15 console.log( 3 * -5); // -15 console.log(-3 * -5); // 15
The Increment operator (++) increments its operand by one. If used as a postfix, then it returns the value before incrementing. If used as a prefix, then it returns the value after incrementing. //postfix var a = 5, // 5 b = a++, // 5 c = a // 6 In this case, a is incr...
The decrement operator (--) decrements numbers by one. If used as a postfix to n, the operator returns the current n and then assigns the decremented the value. If used as a prefix to n, the operator assigns the decremented n and then returns the changed value. var a = 5, // 5 b = a...
ConstantsDescriptionApproximateMath.EBase of natural logarithm e2.718Math.LN10Natural logarithm of 102.302Math.LN2Natural logarithm of 20.693Math.LOG10EBase 10 logarithm of e0.434Math.LOG2EBase 2 logarithm of e1.442Math.PIPi: the ratio of circle circumference to diameter (π)3.14Math.SQRT1_2Square ro...
Given a list comprehension you can append one or more if conditions to filter values. [<expression> for <element> in <iterable> if <condition>] For each <element> in <iterable>; if <condition> evaluates to True, add <expression> (usually a function...

Page 17 of 826