Giter Club home page Giter Club logo

wtfjs's Issues

No need for semicolon

Nice example of interpretator stupidness 👯 (taken from http://lurkmore.to/Javascript )

function foo(){}
foo()
[1,2,3].map(console.log.bind(console)) // Uncaught TypeError: undefined is not an object

however this works:
function foo(){}
foo()
;[1,2,3].map(console.log.bind(console))

{} + {}

In my browser console (firefox) {} + {} does not give "[object Object][object Object]" but Nan.

This is because the first {} is not treated as an object, but and empty code block.
This does not do anything, so what remains is +{} and since an empty object can't be turned into a number that returns NaN.

This only happens when the first {} is at the start of a statement.
When I do console.log({} + {}) it just prints [object Object][object Object]

Apart from the browser console you can also get this quirk using eval: eval("{} + {}") will return NaN also in the nodejs console.

Accessing objects with arrays

I find this one really funny as well:

var obj = { prop: 3 }
obj[['prop']] // -> 3

Of course the explanation is easy, the bracket operator converts everything you pass to a string, and converting an 1 element array to string is just returning the conversion of that element to string.

Improve the explanations of examples

Motivation: If you are a beginner, you can use this notes to get deeper dive into the JavaScript. I hope this notes will motivate you to spend more time reading the specification.

I'm beginner in JS, even though i understands most of the examples but the problem is, most of the examples contains ECMA-Spec as a reference for the detailed explanation of the example which is not a beginner friendly approach.

So what i suggest is try to add explanation of examples there only, so that newbies can understands it properly.

And thanks for all the examples. It really improved my knowledge.

Hope you consider it as a feature request.

An error in the paper

I find an error in the explanation of 'true is false':

true == 'true'    // -> true
false == 'false'  // -> false

Actually, the result of true == 'true' should be 'false'. Because true(boolean) is saw as 1(number),but 'true'(string) is saw as NaN(number), they are different.

multiple assignement with objects

var foo = {n: 1};
var bar = foo;
foo.x = foo = {n: 2}

foo.x // undefined
foo    // {n: 2}
bar    // {n: 1, x: {n: 2}}

foo.x == undefined, but bar.x == foo

more wtfjs

1 + "1" is "11"
But,
1 - "1" is 0


null == 0 is false
null < 0 is false
null <= 0 is true

instanceof between iframes

If you're clever enough to get some javascript array that was created inside an iframe, then in the parent window you'll get

thatArrayFromIframe instanceof Array
> false

code does not what you think! Or am I the only one?

Ran into this while coding... What do you think the below code will do?

var arr = [1,2,3,4,5,6,7,8,9];
function wtfJS(){
  let num, num2, els = arr, 
      len = els.length, 
      el = els[num = Math.floor(Math.random()*len)], 
      el2 = els[
        (num =! (num2 = Math.floor(Math.random()*len)))
        ?num2:num?--num:++num];
  console.log(num,num2,el, el2);
}

It'll either print out 1 # # 2 or true # # 1. The # # are other number that is not important. What is, is the print of "true" or "1" through the num variable which in turn give me either 1 or 2 through the el2 variable. I think it got something to do with coercer in the turner operand.

function okayJS(){
  let num, num2, els, len, el, el2, animLghts;
  els = arr; 
  len = els.length;
  num = Math.floor(Math.random()*len);
  el = els[num];
  num2 = Math.floor(Math.random()*len);
  if (num == num2) num2 = (num?--num:++num);
  el2 = els[num2];
  console.log(num,num2,el, el2);
}

Uncensored F*ck Detected

On the topic parseInt is a bad guy, you missed an , censoring the F word.
"The f in 'f
ck' is hexadecimal 15."
It's not issue, but doesn't fits appropriately with the article.

more array equality

Array equality is a monster in JS, think below:

[] == ''  // true
[] == 0  // true
[''] == ''  // true
[0] == 0  // true
[0] == ''  // ----> false
[''] == 0  // ----> true??

[null] == ''  // true
[null] == 0  // true
[undefined] == ''  // true
[undefined] == 0  // true

[[]] == 0  // true
[[]] == ''  // true

[[[[[[]]]]]] == ''  // true
[[[[[[]]]]]] == 0  // true

[[[[[[ null ]]]]]] == 0  // true
[[[[[[ null ]]]]]] == ''  // true

[[[[[[ undefined ]]]]]] == 0  // true
[[[[[[ undefined ]]]]]] == ''  // true

You should be very careful for above!

{} + [] // -> 0 | explanation is misleading/incorrect

explanation behind that says it's just coercion of one type plus another.
What is happening there is actually a code block and a unary + which coerces the array into 0.
({} + []) would get the same as ([] + {}) understandably.

Class Extends Function: Extra Newness

I present this as an oddity for your amusement.

class Foo extends Function {
  constructor(val) {
    super();
    this.prototype.val = val;
  }
}

new new Foo(':D')().val // => ':D'

Constructors in Javascript are of course just functions with some special treatment. by extending Function using the ES6 class syntax you create a class that, when instantiated, is now a function, which you can then additionally instantiate.

While not exhaustively tested, I believe the last JS statement can be analyzed thus:

(new (new Foo(':D'))()).val
(new newFooInstance()).val
veryNewFooInstance.val
':D'

As a tiny addendum, doing new Function('return "bar";') of course creates a function with the body return "bar";. Since super() in the constructor of our Foo class is calling Function's constructor, it should come as no surprise now to see that we can additionally manipulate things in there.

class Foo extends Function {
  constructor(val) {
    super(`
      this.val = arguments[0];
    `);
    this.prototype.val = val;
  }
}

var foo = new new Foo(':D')('D:');
foo.val // -> 'D:'
delete foo.val; // remove the instance prop 'val', deferring back to the prototype's 'val'.
foo.val // -> ':D'

Translation Request: Indonesian

hello Mr., I am a translator, I want to translate English into Indonesian, Because the majority of Indonesian people do not understand English.. I will be sincere if I am in trust

About the "arguments"

function a(x) {
  arguments[0] = 'hello';
  console.log(x);
}

a(); // -> undefined
a(1); // -> "hello"

when to use toString() and valueOf()?

Example(Unexpected result):

var foo = {
toString: function() { return 'foo'},
valueOf: function() { return 5}
}
console.log(foo + 'bar'); //5bar
console.log([foo,'bar'].join('')); //foobar

NaN and undefined in left-hand side expressions, and some Number.NaN stuff

NaN++ // NaN
0/0 // NaN
(0/0)++ // invalid lhs, but [0/0][0]++ works of course
NaN = void 0 // undefined
--undefined // NaN
--void 0 // invalid lhs

Bonus fun with the read-only Number.NaN property:

Object.is(Number.NaN, Number.NaN) // true
Object.is(Number.NaN, NaN) // true
Object.is(NaN, NaN) // true
Number[NaN] // NaN
Number = {NaN: 0} // works in browsers (modifies window.Number), but reassigning Number crashes Node.js v10.2.0 with "TypeError: Number.isSafeInteger is not a function"
Number.NaN // 0
Number.NaN = 1 // 1
Number.NaN // 1

Explanation to baNaNa

The explanation to "baNaNa" does indicate that the second plus is actually an unary plus operator, which is (in my opinion) key to understanding the trick, yet the reference only lists the binary plus (numeric addition/string concatenation), which is not as important and is a completely different operator.

Here is the reference to unary plus:

12.5.6 Unary + Operator

Another Typo

On line 70 you have

I hope this notes will motivate you to spend more time reading the specification.

This should be these. You also should link to the spec

Further wtf-ness with NaN being type number

I thought you could expand on the NaN being a number type a little.

Type of NaN is a 'number':

typeof NaN; // -> 'number'
+ isNaN(NaN); // -> true

this is almost seemingly contradicting itself because it tells you that NaN is type number, but that NaN is indeed not a number. i understand it makes sense but at first glance i think it makes you think "wtf?"

also, NaN === NaN or NaN == NaN is false both times but may not be as applicable to this section

Translation Request: pt-Br

I'd like to help translating to brazilian portuguese. There is any procedure or guidelines I should follow?

NaN instanceof Number // -> false

Primitive types aren't instances of object wrappers.

42 instanceof Number; //false
Symbol() instanceof Symbol; // false
'foo bar' instanceof String; // false
false instanceof Boolean; // false

Array sort default behavior

I am curious if the default behaviour of array.sort method worth a WTF?

> [0,1,2,3,4,5,6,7,8,9,10].sort()
[ 0, 1, 10, 2, 3, 4, 5, 6, 7, 8, 9 ]

Explanation:
According to documentation the syntax is arr.sort([compareFunction]) and compareFunction - is an optional paramter, if ommited the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

To correctly sort an array of ints valid compareFunction must be provided:

> [0,1,2,3,4,5,6,7,8,9,10].sort((a,b) => a - b)
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.