So I'm new to OOP in javascript, and I'm working on an angularjs site. I have an object being created, and my object methods change the properties, but the propeties are only being changed in the class and not the new object.
//Class
Var Item = function() {
this.currentItem = 1;
}
Item.prototype.itemUp = function(){
this.currentItem++;
}
//New Object
item = new Item();
$scope.currentItem = item.currentItem;
item.itemUp();
After doing some debugging, I realized that this code updates the Item.currentItem, but not the item.currentItem
console.log(item.currentItem) --> 1
console.log(Item.currentItem) --> 2
How to I make the class method modify the new created object, and not the class itself?
Thanks,
Try
$scope.item = new Item();
$scope.item.itemUp();
The error here is, i believe, that item.currentItem
itself is not a reference, but the number itself.
Try this code:
//Class
var Item = function() {
this.itemNumber = 1;
}
Item.prototype.itemUp = function(){
this.itemNumber++;
}
//New Object
item = new Item();
$scope.currentItem = item;
item.itemUp();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.