Thursday, April 23, 2015

Interesting examples of Javascript

I am learning Node.js recently and this is the first time I writing Javascript code. Here are some interesting examples I found when I am learning.

1. Hoisting
var foo = 1;
function bar(){
    if(!foo){
        var foo = 10;
    }
    
   alert(foo);
}
bar();
//answer: 10
2. Hoisting
function test(){
   foo(); // TypeError "foo is not a function"
   bar(); // "this will run!"
   var foo = function () { // function expression assigned to local variable 'foo'
      alert("this won't run!");
   }
   function bar() { // function declaration, given the name 'bar'
      alert("this will run!");
   }
}
test();
3. Closure
for (var i = 0; i < 5; i++){
   setTimeout(function () {
   console.log(i);
   }, 5);
}
//will print five 5

for (var i = 0; i < 5; i++) {
   (function (idx) {
      setTimeout(function () {
         console.log(idx);
      }, 5);
   })(i);
}
//will print 1,2,3,4,5
4. Scoping
(function(){
   var a = b = 5;
})();
console.log(b)
//output: 5, b is global
5. this
var fullname='John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio',
      getFullName: function(){
         return this.fullname;
      }
   }
};
console.log(obj.prop.getFullName());
var test = obj.prop.getFullName;
console.log(test());
console.log(test.call(obj.prop));
//output: 
//'Aurelio'
//'John Doe'
//'Aurelio'
Reference:
5-typical-javascript-interview-exercises
JavaScript-Scoping-and-Hoisting
alsotang node lessons

Python生成知乎专栏RSS订阅

将关注的专栏文章生成XML然后方便订阅到Feedly

1. 直接使用GAE支持的webapp2和jinja2(之前想尝试一下flask结果GAE不支持- -)调用知乎专栏API返回json数据,处理提取得到xml即可。如道哥的黑板报
2. Google App Engine Configure &amp; Deploy.

Code: zhihuzhuanlanRSS-Github 
Reference: 知乎专栏RSS

Instagram auto like a new post of friend's

Google App Engine 提供的免费服务可以很容易的拿来实现一些自己的小想法,比如下面的两个例子。
Instagram自动点赞
自动为你Instagram上关注的一个或几个朋友发布的新照片点赞

1. 注册成为[Instagram Developer](http://instagram.com/developer). Register Client 获得Client ID 和 Client Secret.
2. 在[Google App Engine](https://appengine.google.com/)上新建一个Application.
3. 调用Instagram的API 实现点赞功能。首先在[Jelled](http://jelled.com/instagram/lookup-user-id)上获得需要关注点赞用户的UserID. 获得用户的info, 然后找到用户最新发布的一张照片,判断是否已经点赞,若是没有,则调用like的API点赞。将整个脚本用GAE支持的webapp2 RequestHandler实现get和post。我用的是Instagram的 Media Endpoints 和 Like Endpoints API.
https://api.instagram.com/v1/users/UserID/media/recent/?access_token=ACCESS-TOKEN&count=1
返回的是Json数据,从返回的数据中得到mediaID,也就是关注用户最新照片的ID。然后like这张图片。
setLike_req = "https://api.instagram.com/v1/media/%s/likes" %(mi)
values = {'access_token':ACCESS_TOKEN}
setLike_return = json.loads(urllib2.urlopen(urllib2.Request(setLike_req,urllib.urlencode(values))).read())
4. Google App Engine Configure, Deploy. GAE方便之处就在于configure和deploy都很简单,只需在app.yaml中添加application即可。
application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
  script: likeinsta.application
然后建立后台cron job,只要新建一个cron.yaml然后可设置schedule时间,如every 1 mins,程序每隔一分钟检测一下关注对象的Instagram有没有新照片。
cron: 
- description: main job 
  url: /likeinsta 
  schedule: every 1 mins 
Code: likeinsta-Github
Useful link: python-instagram
Related: 为女神微博点赞