2016-10-15 2 views
1
function Point() { 
    this.xPos = 0; 
    this.yPos = 0; 
} 

Object.__defineGetter__.call(Point.prototype, "getPoint", function(){ 
    return "X: " + this.xPos + " Y: " + this.yPos; 
}); 

Object.__defineSetter__.call(Point.prototype, "setPoint", function(point){ 
    var parts = point.toString().split(', '); 
    parts[0] = this.xPos; 
    parts[1] = this.yPos; 
}); 

var newPoint = new Point(); 

newPoint.setPoint("44.5, 60.6"); 

console.log(newPoint.getPoint); 

Это возвращает мне сообщение об ошибке: newPoint.setPoint не является функцией. Не поймите, почему вы можете мне помочь? Пытается обработать сеттер и геттер.Ошибка Setter и Getter

+1

Почему вы не делаете «Point.prototype.getPoint = function() {...' вместо – adeneo

+0

Эти функции устарели, так или иначе: http://stackoverflow.com/questions/6825191/what-are- функции definegetter-и-definesetter-. Используйте 'get' или' set'. – Terry

+0

Разве это же? Извините i'm новичок в javascript. Спасибо за совет :) –

ответ

4

Основная проблема, с которой вы сталкиваетесь, заключается в том, что сеттеры вызываются с помощью оператора присваивания =.

newPoint.setPoint = "44.5, 60.6"; 

function Point() { 
 
    this.xPos = 0; 
 
    this.yPos = 0; 
 
} 
 

 
Object.__defineGetter__.call(Point.prototype, "getPoint", function(){ 
 
    return "X: " + this.xPos + " Y: " + this.yPos; 
 
}); 
 

 
Object.__defineSetter__.call(Point.prototype, "setPoint", function(point){ 
 
    var parts = point.toString().split(', '); 
 
    // the assignment to this.xPos and this.yPos was the wrong way around 
 
    this.xPos = parts[0]; 
 
    this.yPos = parts[1]; 
 
}); 
 

 
var newPoint = new Point(); 
 

 
// a setter is called by assigning a value to it 
 
newPoint.setPoint = "44.5, 60.6"; 
 

 
console.log(newPoint.getPoint);

Вы также можете использовать стандартный API из Object.defineProperty или Object.defineProperties, который будет легче следовать за кем-либо еще, глядя на код.

Object.defineProperty(Point.prototype, "getPoint", { 
    get: function(){ 
    return "X: " + this.xPos + " Y: " + this.yPos; 
    }, 
    set: function() { 
    // stuff 
    } 
}); 

или ES6

class Point { 
    constructor() { 
    this.xPos = 0 
    this.yPos = 0 
    } 
    get getPoint() { 
     // stuff 
    } 
    set setPoint() { 
    // stuff 
    } 
} 
0

Из того, что я читал defineGetter и defineSetter не используются гораздо больше. Вы могли бы сделать что-то вроде этого:

function Point() { 
    this.xPos = 0; 
    this.yPos = 0; 
} 

Object.defineProperties(Point, { 
    xPos: { 
    set: function(newValue) { 
     this.xPos = newValue; 
    }, 

    get: function() { 
     return this.xPos; 
    } 
    }, 

    yPos: { 
    set: function(newValue) { 
     this.yPos = newValue; 
    }, 

    get: function() { 
     return this.yPos; 
    } 
    } 
}); 

newPoint = new Point(); 
console.log(newPoint.xPos); 
newPoint.xPos = 45; 
console.log(newPoint.xPos); 

Вы можете прочитать больше об использовании Object.definePropertieshere.