10 hours ago, came across atrick-wied’s ninja object properties in JavaScript, that speaks of code as below:
var x = {};
x[""] = 1;
x[" "] = 2;
x[" "] = 3;
console.log(x);
x = {};
x[String.fromCharCode(1)] = 1;
x[String.fromCharCode(2)] = 2;
x[String.fromCharCode(3)] = 3;
x[String.fromCharCode(4)] = 4;
console.log(x);
This is not very obfuscated JS-code, but indeed fun to see spaces as object properties and space as ✐
On the same lines, out of interest tried exploring the same property in other languages:
Python:
>>> class A(object): pass
>>> a = A()
>>> setattr(a, ' ', 1)
>>> setattr(a, ' ', 2)
>>> setattr(a, '', 3)
>>> getattr(a, '')
3
Ruby:
irb(main):001:0> a = {}
=> {}
irb(main):002:0> a[' '] = 1
=> 1
irb(main):003:0> a[' '] = 2
=> 2
irb(main):004:0> a[' ']
=> 1
irb(main):005:0> a[' ']
=> 2
Perl:
$x = {};
$x->{' '} = 1;
say $x->{' '};
$x->{' '} = 2;
say $x->{' '};
$x->{' '} = 3;
say $x->{' '};
So it appears most of the major scripting languages have the ninja object property in them!
#javascript#python#ruby
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.