Skip to content

Instantly share code, notes, and snippets.

@andineck
Created April 1, 2015 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andineck/d500fe17d3b4b6df5b3d to your computer and use it in GitHub Desktop.
Save andineck/d500fe17d3b4b6df5b3d to your computer and use it in GitHub Desktop.
javascript adding properties to function, string and array
function echo(input) { console.log(input);}
undefined
echo('hallo')
VM304:2 hallo
undefined
// you can add an attribute to a function
echo.prop = 'echooo'
"echooo"
echo.prop
"echooo"
// you can't add an attribute to a literal string
var a = 'asdf'
undefined
a.prop = 'qwer'
"qwer"
a.prop
undefined
// but you can add an attribute to a new String() object
var a = new String('asdf')
undefined
a.prop = 'qwer'
"qwer"
a.prop
"qwer"
// and you can as well add an attribute to an array
var a = [1,2,3]
undefined
a.prop = 'qwer'
"qwer"
a.prop
"qwer"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment