Tag: JS

Set a default parameter value for a JavaScript function

First Solution

From ES6/ES2015, default parameters is in the language specification.

function read_file(file, delete_after = false) {
  // Code

just works.

Reference: Default Parameters – MDN

Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed.

Pre ES2015,

There are a lot of ways, but this is my preferred method – it lets you pass in anything you want, including false or null. (typeof null == "object")

function foo(a, b)
  a = typeof a !== 'undefined' ? a : 42;
  b = typeof b !== 'undefined' ? b : 'default_b';

Another Solution

To anyone interested in having there code work in Microsoft Edge, do not use defaults in function parameters.

function read_file(file, delete_after = false) {

In that example Edge will throw an error “Expecting ‘)'”

To get around this use

function read_file(file, delete_after) {
  if(delete_after == undefined)
    delete_after = false;

As of Aug 08 2016 this is still an issue



Reference link: http://stackoverflow.com/questions/894860/set-a-default-parameter-value-for-a-javascript-function


Ruby on Rails tips

1. Use  Javascript and Jquery with Rails

  • Install jquery rails:
    For this add following code in gam file
    gem 'jquery-rails'
    Install gem (Run on command prompt)
    $bundle install
  • Install jquery package (Run on command prompt)
    $rails generate jquery:install
  • Add following code in layout/application.html.erb
    <%= javascript_include_tag(:defaults) %>

2. Use PostgreSQL with rails

  • Install PostgreSQL database (Run on command prompt)
    $sudo apt-get install postgresql
  • Create rails application with postgresql database (Run on command prompt)
    rails new pg -d postgresql
  • Install bundle
    bundle install

3. Use Pagination with Rails (using will-paginate gem)

  • Add will_paginate gem in gem file
    For this add following code in gam file
    gem 'will_paginate'
  • Install bundle
    $bundle install
  • Use following code instead of Model.all
    @data = Model.paginage(:page => params[:page], :per_page => 10)
  • Include following code in required view
    <%= will_paginate @category %>
  • Remember that all other codes remain same in controller, model and views

4. Rename rails application:

  • First install grep if not installed
    $sudo apt-get install grep
  • Then execute  $grep -ir old_name * and make sure old_name appears only in places you want to replace, then do:
  • $replace old_name new_name -- `grep -r old_name * | cut -d ':' -f 1`
  • Which changes the old_name to new_name in following files
    • config/application.rb
    • config/environment.rb
    • config/environments/development.rb
    • config/environments/production.rb
    • config/environments/test.rb
    • config/initializers/secret_token.rb
    • config/initializers/session_store.rb
    • config/mongoid.yml (if using Mongoid)
    • config/routes.rb
    • config.ru
    • Rakefile
    • app/views/layouts/application.html.erb, in title tag