Tutorial by Examples: ar

A normal function declaration looks like this: function foo(){ } A function defined like this is accessible from anywhere within its context by its name. But sometimes it can be useful to treat function references like object references. For example, you can assign an object to a variable based...
There are many ways to create arrays. The most common are to use array literals, or the Array constructor: var arr = [1, 2, 3, 4]; var arr2 = new Array(1, 2, 3, 4); If the Array constructor is used with no arguments, an empty array is created. var arr3 = new Array(); results in: [] Note...
Spread operator 6 With ES6, you can use spreads to separate individual elements into a comma-separated syntax: let arr = [1, 2, 3, ...[4, 5, 6]]; // [1, 2, 3, 4, 5, 6] // in ES < 6, the operations above are equivalent to arr = [1, 2, 3]; arr.push(4, 5, 6); The spread operator also act...
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 ...
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (xhttp.readyState === XMLHttpRequest.DONE && xhttp.status === 200) { //parse the response in xhttp.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(...
Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, f...
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...
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...
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...
An array can be initialized empty: // An empty array $foo = array(); // Shorthand notation available since PHP 5.4 $foo = []; An array can be initialized and preset with values: // Creates a simple array with three strings $fruit = array('apples', 'pears', 'oranges'); // Shorthand no...
Anonymous functions can be assigned to variables for use as parameters where a callback is expected: $uppercase = function($data) { return strtoupper($data); }; $mixedCase = ["Hello", "World"]; $uppercased = array_map($uppercase, $mixedCase); print_r($uppercased); ...
The use construct is used to import variables into the anonymous function's scope: $divisor = 2332; $myfunction = function($number) use ($divisor) { return $number / $divisor; }; echo $myfunction(81620); //Outputs 35 Variables can also be imported by reference: $collection = []; $a...
print_r() - Outputting Arrays and Objects for debugging print_r will output a human readable format of an array or object. You may have a variable that is an array or object. Trying to output it with an echo will throw the error: Notice: Array to string conversion. You can instead use the print_r...
A single case in a switch statement can match a range of values. let number = 20 switch number { case 0: print("Zero") case 1..<10: print("Between One and Ten") case 10..<20: print("Between Ten and Twenty") case 20..<30: print("Be...
dictionary = {"Hello": 1234, "World": 5678} print(dictionary["Hello"]) The above code will print 1234. The string "Hello" in this example is called a key. It is used to lookup a value in the dict by placing the key in square brackets. The number 1234 is ...
Arguments are passed to the program in a manner similar to most C-style languages. $argc is an integer containing the number of arguments including the program name, and $argv is an array containing arguments to the program. The first element of $argv is the name of the program. #!/usr/bin/php p...
The git clone command is used to copy an existing Git repository from a server to the local machine. For example, to clone a GitHub project: cd <path where you'd like the clone to create a directory> git clone https://github.com/username/projectname.git To clone a BitBucket project: cd ...
Consider a database with the following two tables. Employees table: IdFNameLNameDeptId1JamesSmith32JohnJohnson4 Departments table: IdName1Sales2Marketing3Finance4IT Simple select statement * is the wildcard character used to select all available columns in a table. When used as a substitute f...
The JSONSerialization class is built into Apple's Foundation framework. 2.2 Read JSON The JSONObjectWithData function takes NSData, and returns AnyObject. You can use as? to convert the result to your expected type. do { guard let jsonData = "[\"Hello\", \"JSON\"]&q...
This example assumes Ruby and Ruby on Rails have already been installed properly. If not, you can find how to do it here. Open up a command line or terminal. To generate a new rails application, use rails new command followed by the name of your application: $ rails new my_app If you want to c...

Page 5 of 218