Javascript notes

  • Functions are normally used in combination with events.
  • document:Each web page loaded in the browser has its own document object. This object serves as an entry point to the web page’s content (the DOM tree, including elements such as <body> and <table> ) and provides functionality global to the document (such as obtaining the page’s URL and creating new elements in the document)。 SEE HERE
  • Define a new Data Type,

function TypeName( [args] ) { this.args = args; }

it uses keyword function to define datatype TypeName. Its constructor uses optional arguments. And these attributes can be obtained by var myType=new TypeName(name); Document.write(

Note that in JS, there is no conception of class.

  • define a new variable by defining a function: it actually defines a new object

var a_obj = new function(param1, param2)




Rails can work without it, what does schema.rb really do?

It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it’s hard to deduct the schema just from the migrations alone. With a present schema.rb, you can just have a look there. ActiveRecord itself will indeed not use it. It will introspect the database during runtime as this is much safer than to expect users to keep the schema.rb up-to-date. However to avoid confusion of your developers, you should always maintain a file that is up-to-date with your migrations.


Use “br annotate” to update the comments of tables’ columns in each model file.


irb=> “1234”.split(”)
=> [“1”, “2”, “3”, “4”]

=> “1234”
irb=> [1,2,3,4].join(‘,’)
=> “1,2,3,4”

inject(p1 = v1, p2 = v2) public

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.

If you do not explicitly specify an initial value for memo, then uses the first element of collection is used as the initial value of memo.


# Sum some numbers
(5..10).reduce(:+)                            #=> 45
# Same using a block and inject
(5..10).inject {|sum, n| sum + n }            #=> 45
# Multiply some numbers
(5..10).reduce(1, :*)                         #=> 151200
# Same using a block
(5..10).inject(1) {|product, n| product * n } #=> 151200
# find the longest word
longest = %w{ cat sheep bear }.inject do |memo,word|
   memo.length > word.length ? memo : word
longest                                       #=> "sheep"


  • sth. is worth it if its benefits exceeds its cost
  • we want methods to have only ONE reason to change as requirements of a system change, so that we can make change in small steps and with confidence
  • large class smell is not about the size; it is about responsibilities.
  • if you need to pass some variable to almost every method in a class, make this variable as an instance variable in the initialization.


it tells the double to only call the methods we tell it to expect and ignore any other methods

it “should receive sth.a”
a = double(‘model’)
model =

def start

if we add another example
it “should receive sth.b”
a = double(‘model’)
model =

Both example will be failed
+++++++++++++++++++++solution ++++++++++++++++++
change a = double(‘model’) to a = double(‘model’).as_null_object

call a method from a parent module


module Awesome

   class Test

     def awesome?




#module fcn

  def awesome_detection

    puts “yes”




module Awesome

  class Test

    def awesome?




#module fcn

  def self.awesome_detection

    puts “yes”



scenario outlines

a single scenario outline provides tables of input data and expected output. It consists of a bunch of similar scenario.

Two differences from scenario:

1) use keyword Scenario Outline instead of Scenario

2) Variable data in angle brackets

scenarios keyword is followed by rows of example data.

some convention of ruby

def method

method_a  # the same as self.method_a


method_a must be an instance method, it is equal to calling “self.method_a”

when a name is there without any setting by “=”, it is a method, not a variable.

Example: “hello”*count, here, count can be a variable, or a method without any argument.

btw, “hello”*2=”hellohello”