---
title: "Ninja Object Properties in Scripting languages"
description: "Ninja object properties in JavaScript and other scripting languages - using spaces and control characters as object keys."
url: "https://h3manth.com/scribe/ninja-object-properties-in-scripting-languages/"
canonical_url: "https://h3manth.com/scribe/ninja-object-properties-in-scripting-languages/"
markdown_url: "https://h3manth.com/scribe/blog/ninja-object-properties-in-scripting-languages.md"
publishDate: "2012-06-05T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["javascript","python","ruby"]
word_count: 190
reading_time_minutes: 1
x_markdown_tokens: 364
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Ninja Object Properties in Scripting languages

> Ninja object properties in JavaScript and other scripting languages - using spaces and control characters as object keys.

10 hours ago, came across atrick-wied's ninja object properties in JavaScript, that speaks of code as below:

```javascript
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:**

```python
>>> class A(object): pass
>>> a = A()
>>> setattr(a, ' ', 1)
>>> setattr(a, '  ', 2)
>>> setattr(a, '', 3)
>>> getattr(a, '')
3
```

**Ruby:**

```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:**

```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!**
