Javascript OOP can't get property -
i'm trying rewrite code oop approach, stuck in something. there example :
var counter = { column: { estimation: "tr td:nth-child(3)", worked: "tr td:nth-child(4)", ratio: "tr td:nth-child(5)", difference: "tr td:nth-child(6)" }, columnlength : function display(){ return $(this.column.estimation).length; },
ok, works! if write same, not in function though:
var counter = { column: { estimation: "tr td:nth-child(3)", worked: "tr td:nth-child(4)", ratio: "tr td:nth-child(5)", difference: "tr td:nth-child(6)" }, columnlength : $(this.column.estimation).length },
it doesn't, there error : column isn't declared.
why must in function? thx in advance.
the problem when you're using this
inside jquery selector $(...)
value of global object window
instead of object counter
. so, basically, you're trying call property column of object window window.column
, doesn't exist.
if run following code, you'll see what's value of this
:
var counter = { column: { estimation: "tr td:nth-child(3)", worked: "tr td:nth-child(4)", ratio: "tr td:nth-child(5)", difference: "tr td:nth-child(6)" }, columnlength: console.log(this) }
so, why happening?
because when you're assigning executed function parameter of literal object, function executed in global context.
more info use of this
: http://blog.kevinchisholm.com/javascript/context-object-literals/
Comments
Post a Comment