-3

According to me this is only being used if you need to call constructor like below

var dogObj = function(){
    this.bark = "woof";
}

var dog1 = new dogObj();
console.log(dog1.bark);

Is there any cases where I use this on object literal?

var bark = {
  this.bark = "dog"
}

Is above code valid? If yes,how do I call it?

Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
  • 2
    *"is above code valid?"* Run it! Learn about objects: http://eloquentjavascript.net/04_data.html . – Felix Kling Dec 15 '15 at 07:04
  • I believe you're looking for `var o={msg:'hello world',fn:function(){alert(this.msg)}};o.fn();` – Ultimater Dec 15 '15 at 07:05
  • @Ultimater maybe? but why use this approach compare to constructor? I was asp.net coder. – Jennifer Aniston Dec 15 '15 at 07:12
  • @Ultimater how can I pass custom message to `o` object? – Jennifer Aniston Dec 15 '15 at 07:13
  • Other related dup: [using key value pair of same object inside itself with 'this' in javascript](http://stackoverflow.com/questions/28357785/using-key-value-pair-of-same-object-inside-itself-with-this-in-javascript/28358201#28358201). – jfriend00 Dec 15 '15 at 07:15
  • The idea is that `this` points to whatever context you call the function with. For example you might want to do something like `var o={username:'me'};var fn=function(){alert(this.username)};fn.call(o);`. When you assign a function to an event handler, `this` will point to the HTML element rather than its parent closure's `this` context. There's workarounds like `var t=this;` for this type of situation, but generally this is what call and apply are used for in order to set the context which you want `this` to point to. – Ultimater Dec 15 '15 at 07:18
  • In response to your question on my first example, you could do `o.fn.call({msg:'overriden'});` thereby passing-in a custom message and overriding the bound object which the function is declared in. – Ultimater Dec 15 '15 at 07:22
  • @Ultimater it seems like the only way is to do that. abit messy but still better than constructor like.. – Jennifer Aniston Dec 15 '15 at 07:27
  • 1
    call and apply are very clean in my opinion. If you're doing something dirty with it, requiring multiple closures, perhaps `bind` is what you're looking for. It, in a sense, allows you to "edit" the `this` context of a function and returns the new edited function without calling it. Thus when you're ready to call it, you can call it the normal way like you usually would with the new context bound to the function. – Ultimater Dec 15 '15 at 07:33

2 Answers2

2

you can use

var bark = {
  bark : "dog"
}  

and call it like

console.log(bark.bark)
ozil
  • 6,930
  • 9
  • 33
  • 56
0
this.bark = "woof";

Is not valid.

You have try something like below

var dog = {
  bark : "dog"
}

And then you can access like console.log(dog.bark)

Once you formed any json string you can validate the sting using http://jsonlint.com/ just place your json string and the click on validate.

Anto S
  • 2,448
  • 6
  • 32
  • 50