RSS

Tag Archives: js

js timestamp

Sometimes while logging js I need to have a timestamp in the beggining of the row. There are libraries to manage timestamps and to print wonderful logs, but sometimes the needs are only to make some console.log while debugging.

This is my one-line way to handle it:

function timestamp() {
  return (new Date).toISOString().match(/([0-9]{2}:[0-9]{2}:[0-9]?[0-9].[0-9]{3})/g)[0];
}

or “keep only the time part of an ISO formatted timestamp”.

 
Leave a comment

Posted by on 2016/12/16 in dev

 

Tags: , ,

youtubing

I was studying (rev-eng) how does YouTube works when I’ve found this post about the get_video_info call.

After some digging and some trying I’ve made a node.js module to make the request and parse the response, without the need for the official API.

Here’s the link:
https://github.com/pste/youtube.get-video-info

 
Leave a comment

Posted by on 2016/07/29 in dev

 

Tags: , , , ,

this.window (& friends)

Open a console in your browser, then verify these:

console.log(this === this.window); // true
console.log(this === this.window.window.window); // true
console.log(this === this.self); // true 
console.log(this === this.frames); // true (also when this.frames.length > 0)

When this.frames.length > 0 you can also:

for (i in this.frames) console.log(this === this.frames[i]) // true,false,true,true,false,.... (so many true because we have "self","window","parent","frames",... The others are $,jQuery,google stuff,...)

console.log(this.window[0] === this.window[1]); // false
console.log(this.window[0] === this); // false (obviously in my test)
console.log(this.window[1] === this); // false (obviously in my test)
console.log(this.window[1] === this.frames[1]); // true
 
Leave a comment

Posted by on 2016/04/07 in dev

 

Tags: , , ,

true or false: it’s not a binary choice

When using javascript functions like the Array Filter Method you need to define a test over a value.
Sometimes I see that developers are confused about “what’s true” and this can generate bugs.

Let’s work with an example.
Given a function that makes some test over a parameter:

// be careful: uneval() is a Firefox non-standard feature
function doTheTest(val) {
    console.log("------------- val " + uneval(val) + " is " + typeof (val));

    if (val)  console.log('val is pseudo true');
    else console.log('val is not pseudo true');
    
    if (val == true)  console.log('val is == true' );
    else console.log('val is not == true');
    
    if (val === true)  console.log('val is === true' );
    else console.log('val is not === true');
    
    if (val != true)  console.log('val is != true' );
    else console.log('val is not != true');
    
    if (val !== true)  console.log('val is !== true' );
    else console.log('val is not !== true');

    // ..and null?
    if (val == null)  console.log('val is == null' );
    else console.log('val is not == null');
    if (val === null)  console.log('val is === null' );
    else console.log('val is not === null');
}

let’s call that function with some “stuff”:

doTheTest(42);
doTheTest(0);
doTheTest("one");
doTheTest("");
doTheTest(null);
doTheTest(); // undefined
doTheTest({a:1, b:11});
doTheTest(doTheTest); // =)

This is the Firebug’s console output, with some highlight from me:

------------- val 42 is number
val is pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val 0 is number
val is not pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val "one" is string
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val "" is string
val is not pseudo true            <==
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val null is object
val is not pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is == null
val is === null
------------- val (void 0) is undefined
val is not pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is == null            <==
val is not === null
------------- val ({a:1, b:11}) is object
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null
------------- val function doTheTest(val) { ..code removed.. } is function
val is pseudo true
val is not == true
val is not === true
val is != true
val is !== true
val is not == null
val is not === null

Did you expect these results?

 
Leave a comment

Posted by on 2013/11/06 in dev

 

Tags: , , , , , ,

Iterating object literals

One of the wonderful things of the javascript language is that you can easily do complicate things. One of these is working with ‘objects’.
One kind of ‘object’ is the object literal: just a list of name / value properties.

Here’s an using sample:

// declaring two object literals
var a = { x: 1, y:2 };
var b = { y:10, z: 20};

// dump 'a' into the console
for (p in a) console.log(p.toString() + '' + a[p].toString());

// extend 'a' with 'b'
for (p in b) a[p] = b[p];
 
Leave a comment

Posted by on 2013/03/22 in dev

 

Tags: ,