Monthly Archives: February 2016

new vs non-new object creation

http://code.tutsplus.com/tutorials/fully-understanding-the-codethiscode-keyword–net-21117

note:

The Global Object

When a function is called without an owner object, the value of this becomes the global object.

In a web browser the global object is the browser window.
This example returns the window object as the value of this:

standalone Function (notice no new!)

The reason why window.name logs Cody Lindley is because the ‘this’ inside the function references the window global object. Hence when we attach the attribute name to this, it attaches name to the window object. the ‘this’ has nothing to do with the object Person.

new Object

Now, when we use new Object, we create an object on the heap, and this in our function refers to that object. IT IS NOT connected to the (global or window) object anymore

Hence, when we log cody.name, it works because cody is that object in the heap. The this in the function, refers to cody.

We assign name attribute to the object cody.

Also, take note that the this properties are public because there is no closure. If we create a closure (by creating inner functions), then the properties attached to this will then be private.

Another example:

const object, changing attributes

http://stackoverflow.com/questions/30314457/how-to-make-function-parameter-constant-in-javascript

Const will only prevent re-assigning

It will not prevent modification of the attributes of the object.

For example, this will give you an error because you are trying to reassign the variable car to another object.

But if you were to change the attribute of the object, it is valid:


You’d see honda, white

http://stackoverflow.com/questions/30314457/how-to-make-function-parameter-constant-in-javascript

Another example:

However, bear in mind that const in ES6 notation does not make the variable immutable.

is a completely valid program and a will become [1, 2, 3]. const will only prevent you from reassigning a, so that you can’t do a = [] or a = {} or whatever once const a = [1, 2]; already defined (in that particular scope).

Read Only Variable

ref – http://stackoverflow.com/questions/2821509/can-you-use-constant-variables-in-javascript

if you’re looking for a read-only variable, you simulate that with something like

and then use it like

Installing JSLint in sublime

ref – https://github.com/devdoc/SublimeLinter-jslint
https://www.npmjs.com/package/nodelint

First, make sure you install the jslint npm package

npm install -g jslint

Install Package control

https://packagecontrol.io/installation

In your sublime, View > Console.
Then copy and paste this into the console and press enter:

Then after that, In Sublime,

Tools > Command Palette

type “Install” and type enter.
You will t hen see a menu popup with all the packages.

Look for SublimeLinter-jslint

Logging Cron Job

ref – http://stackoverflow.com/questions/4811738/cron-job-log-how-to-log

http://stackoverflow.com/questions/15395479/why-ive-got-no-crontab-entry-on-os-x-when-using-vim

Getting started with Cron
  • In Terminal: crontab -e.
  • Press i to go into vim’s insert mode.
  • Type your cron job, for example:

    30 * * * * /Users/rickytsao/Desktop/run_email_to_admin >> /Users/rickytsao/Desktop/chron-log.txt
  • Press Esc to exit vim’s insert mode.
  • Type ZZ (must be capital letters).
  • You should see the following message: crontab: installing new crontab. You can verify the crontab file by using crontab -l.
Logging Cron jobs


30 * * * * /Users/rickytsao/Desktop/run_email_to_admin >> /Users/rickytsao/Desktop/chron-log.txt

cURL

ref – http://makandracards.com/makandra/1145-how-to-send-http-requests-using-curl


curl http://localhost:6680/results -GET

to send a HTTP request

Download a Single File

The following command will get the content of the URL and display it in the STDOUT (i.e on your terminal).


$ curl http://www.centos.org

ryh:Desktop rickytsao$ curl http://www.centos.org

301 Moved Permanently

301 Moved Permanently


nginx/0.8.55



ryh:Desktop rickytsao$

Download file and store in file

To store the output in a file, you an redirect it as shown below. This will also display some additional download statistics.

$ curl http://www.centos.org > centos-org.html

self does not hold strong ref to dispatch_async

ref – https://digitalleaves.com/blog/2015/05/demystifying-retain-cycles-in-arc/

Examples of non-cycles

      Using dispatch_async as the example is misleading as self does not hold a strong reference to it so, there is no threat of a retain cycle.

    • The block retains self, but self doesn’t retain the block. If one or the other is released, no cycle is created and everything gets deallocated as it should.

    How you get into trouble

    Where you get into trouble is something like:

    Now, your object (self) has an explicit strong reference to the block. And the block has an implicit strong reference to self. That’s a cycle, and now neither object will be deallocated properly.

Badminton Events App notes

Sort database by date

Sort the database by date

db.badmintonEvents.find().sort({“date”:1});

Script to send admin result email


curl http://localhost:6680/results -GET

send_admin_email_script

to run: ./send_admin_email_script

attaching files to gunemail

DB manipuluation

mongo
show dbs
use BadmintonEventsDatabase

db.badmintonEvents.drop()

To Drop an Event


db.badmintonEvents.remove({“_id” : ObjectId(“5704d3df5b4e17c2a90b558c”)});

db.badmintonEvents.find()

When moving over to production

change ip and port in config
change ip and port in client.js

to get results
http://www.rickytsao.com:8881/results.html

to get registeration page
http://www.rickytsao.com:8881/welcome.html

Mongod Address already in use for socket

Problem

Last login: Fri Feb 12 10:54:07 on ttys002
ryh:~ rickytsao$ mongod
2016-02-13T11:07:24.914+0800 E NETWORK [initandlisten] listen(): bind() failed errno:48 Address already in use for socket: 0.0.0.0:27017
2016-02-13T11:07:24.914+0800 E NETWORK [initandlisten] addr already in use
2016-02-13T11:07:24.941+0800 W – [initandlisten] Detected unclean shutdown – /data/db/mongod.lock is not empty.
2016-02-13T11:07:25.001+0800 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to lock file: /data/db/mongod.lock errno:35 Resource temporarily unavailable. Is a mongod instance already running?, terminating
2016-02-13T11:07:25.004+0800 I CONTROL [initandlisten] dbexit: rc: 100

Solution

Check to see what mongo database instance is running on port 27017


ryh:~ rickytsao$ sudo lsof -i :27017

Password:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 442 rickytsao 5u IPv4 0xd82daecd96900a0b 0t0 TCP *:27017 (LISTEN)


ryh:~ rickytsao$ KILL -9 442

Then, start up your mongo database

ryh:~ rickytsao$ mongod

2016-02-13T11:10:14.375+0800 W – [initandlisten] Detected unclean shutdown – /data/db/mongod.lock is not empty.
2016-02-13T11:10:14.384+0800 I JOURNAL [initandlisten] journal dir=/data/db/journal
2016-02-13T11:10:14.384+0800 I JOURNAL [initandlisten] recover begin
2016-02-13T11:10:14.408+0800 I JOURNAL [initandlisten] recover lsn: 16539606
2016-02-13T11:10:14.408+0800 I JOURNAL [initandlisten] recover /data/db/journal/j._2
2016-02-13T11:10:14.408+0800 I JOURNAL [initandlisten] recover skipping application of section seq:14612540 < lsn:16539606 2016-02-13T11:10:14.408+0800 I JOURNAL [initandlisten] recover skipping application of section seq:14662560 < lsn:16539606 2016-02-13T11:10:14.408+0800 I JOURNAL [initandlisten] recover skipping application of section seq:14761940 < lsn:16539606 2016-02-13T11:10:14.411+0800 I JOURNAL [initandlisten] recover cleaning up 2016-02-13T11:10:14.411+0800 I JOURNAL [initandlisten] removeJournalFiles 2016-02-13T11:10:14.411+0800 I JOURNAL [initandlisten] recover done 2016-02-13T11:10:14.480+0800 I JOURNAL [durability] Durability thread started 2016-02-13T11:10:14.480+0800 I JOURNAL [journal writer] Journal writer thread started 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] MongoDB starting : pid=85510 port=27017 dbpath=/data/db 64-bit host=ryh.princeton.epam.com 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] db version v3.0.7 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] git version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] build info: Darwin mci-osx108-13.build.10gen.cc 12.3.0 Darwin Kernel Version 12.3.0: Sun Jan 6 22:37:10 PST 2013; root:xnu-2050.22.13~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] allocator: system 2016-02-13T11:10:14.480+0800 I CONTROL [initandlisten] options: {} 2016-02-13T11:10:14.503+0800 I NETWORK [initandlisten] waiting for connections on port 27017