3 Replies - 528 Views - Last Post: 16 February 2014 - 10:14 PM

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Object Define Property In Javacsript Not Work In This Instance Why?

Posted 16 February 2014 - 02:54 AM

So If I Use:

	 function testObject() {
		 	
		 	Object.defineProperty(this, "numberProperty",{
		 	set: function (x) {
		 		this.number = x;
		 	},
		 	get: function() {
		 		return this.number * 2;
		 	},
		 	enumberable: true,
		 	configurable: true
		   });
		 }
		
		 
			$(document).ready(function(){
				var test = new testObject();
				test.numberProperty = 100;
				alert(test.numberProperty);
			});



For example if you wanted the property to return the value doubles something like this:

$(document).ready(function(){
				var test = new testObject();
				test.numberProperty = 100;
				alert(test.numberProperty);
			});



will output 200 ok so that works but what about if you what object define property within a variable object like so:

 var testObject = {
		 	
		 	Object.defineProperty(this, "numberProperty",{
		 	set: function (x) {
		 		this.number = x;
		 	},
		 	get: function() {
		 		return this.number * 2;
		 	},
		 	enumberable: true,
		 	configurable: true
		   });
		 }


$(document).ready(function(){
				var test = testObject;
				test.numberProperty = 100;
				alert(test.numberProperty);
			});



Does not work at ALL. Why? I think there's something I'm missing about Javascript objects and how everything is an object? Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: Object Define Property In Javacsript Not Work In This Instance Why?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Object Define Property In Javacsript Not Work In This Instance Why?

Posted 16 February 2014 - 03:01 AM

Quote

Does not work at ALL. Why?

because in the second case you have an Object literal. and the syntax of Object literals is different from normal code (a key followed by a colon followed by a JS expression, comma-separated between each key-value block)
Was This Post Helpful? 1
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 762
  • Joined: 31-August 11

Re: Object Define Property In Javacsript Not Work In This Instance Why?

Posted 16 February 2014 - 03:28 PM

View PostDormilich, on 16 February 2014 - 03:01 AM, said:

Quote

Does not work at ALL. Why?

because in the second case you have an Object literal. and the syntax of Object literals is different from normal code (a key followed by a colon followed by a JS expression, comma-separated between each key-value block)


Oh that's right I forgot about that man I apologize. So can you still use access properties of get and set with this?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Object Define Property In Javacsript Not Work In This Instance Why?

Posted 16 February 2014 - 10:14 PM

sure
// IIRC
var obj = {
    set a: function(x) { ... },
    get a: function( ) { ... }
};


but you coud modify the above code to use Object.defineProperty
function testObject() {
    var obj = {};
    Object.defineProperty(obj, "numberProperty", ...);
    // ...
    return obj;
}
var obj = testObject();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1