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(myType.name).

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)

 

 

Advertisements

db/schema.rb

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.

inject

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

[1,2,3,4].join
=> “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.

Examples:

# 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
end
longest                                       #=> "sheep"

rules

  • 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.

as_null_object

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

Example:
it “should receive sth.a”
a = double(‘model’)
model = Model.new(a)
a.should_receive(:method_a).with(sth.a)
model.start
end

def start
method_a(sth.a)
method_a(sth.b)
end

+++++++++++++++++++++++++++++++++++++
if we add another example
it “should receive sth.b”
a = double(‘model’)
model = Model.new(a)
a.should_receive(:method_a).with(sth.b)
model.start
end

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

call a method from a parent module

example:

module Awesome

   class Test

     def awesome?

       awesome_detection

      end

    end

#module fcn

  def awesome_detection

    puts “yes”

  end

end

++++++++SOLUTION+++++++++++

module Awesome

  class Test

    def awesome?

      Awesome.awesome_detection

    end

  end

#module fcn

  def self.awesome_detection

    puts “yes”

  end

end