2016-06-09 85 views
0

下面的代码运行正常:原型函数没有定义

function Button(tagName) { 
    var button; 
    if (tagName) button = document.createElement(tagName); 
    else button = document.createElement(div); 

    button.innerHTML = 'Track Order'; 
    button.applyJsonProperties = function(jsonProperties){ 
    if(jsonProperties){ 
     for(var cssAttribute in jsonProperties){ 
     this.style[cssAttribute] = jsonProperties[cssAttribute]; 
     } 
    } 
    } 
    return button; 
} 

下面的代码产生错误

function Button(tagName) { 
    var button; 
    if (tagName) button = document.createElement(tagName); 
    else button = document.createElement(div); 

    button.innerHTML = 'Track Order'; 

    return button; 
} 
Button.prototype.applyJsonProperties = function(jsonProperties){ 
    if(jsonProperties){ 
    for(var cssAttribute in jsonProperties){ 
     this.style[cssAttribute] = jsonProperties[cssAttribute]; 
    } 
    } 
} 

var divButton = new Button('div'); 
var props = { "color" : "blue" } 
divButton.applyJsonProperties(props); //returns undefined function 
+0

'divButton.applyjSonProperties(道具)'错方法名 –

+3

对象从返回*作为构造函数调用的Button *函数不是* Button *的实例,即它不会继承* Button.prototype *,因为它返回一个DOM Bu tton元素,而不是构造函数的* this *。 – RobG

+0

您在这里错过了'div'的引号:'document.createElement(div)'。你可以将代码简化为'tagName = tagName || 'div';'在第一行。 – Barmar

回答

0

因为你在Button函数返回键。

而你的applyjSonProperties什么也没有返回。

尝试这样:

function Button(tagName) { 
 

 
    if (tagName) this.button = document.createElement(tagName); 
 
    else this.button = document.createElement(div); 
 

 
    this.button.innerHTML = 'Track Order'; 
 

 
} 
 
Button.prototype.applyJsonProperties = function(jsonProperties){ 
 
    if(jsonProperties){ 
 
    for(var cssAttribute in jsonProperties){ 
 
     this.button.style[cssAttribute] = jsonProperties[cssAttribute]; 
 
    } 
 
    } 
 
    return this; //return modified (or not) instance 
 
} 
 

 
var divButton = new Button('div'); 
 
console.log(divButton.button); 
 

 
var props = { "color" : "blue" } 
 
divButton.applyJsonProperties(props); 
 
console.log(divButton.button);

+0

谢谢@RobG ...我的错误,我已经更新了答案。 –

+0

@RobG我更新了答案。正如你所看到的那样 - 'divButton'现在是'.button'属性的自定义'Button'“class”的实例 –

+0

是否有可能没有'.button'属性并设置'this = document.createElement(tagName)里面'功能按钮(tagName)'? – guest

1

不知道你正试图在这里做。正如你明确地从构造函数返回一个局部变量按钮,divButton = new Button('div')将是一个div元素,如<div>Track Order</div>。这个元素显然没有applyJsonProperties功能。

0

当一个函数被称为构造函数,其初始化为一个新的对象,从构造函数的原型继承,即对象的内部[[Prototype]]引用构造函数的公共原型财产。

当您返回一个不同的对象(在OP中,一个DOM HTMLButtonElement)时,那么该对象不会从构造函数继承。例如

function Button(tagName) { 
 
    var button = document.createElement(tagName || 'button'); 
 
    return button; 
 
} 
 

 
Button.prototype.applyJsonProperties = function(jsonProperties){}; 
 

 
var button = new Button(); 
 

 
console.log('button instanceof Button?: ' + (button instanceof Button)); // false 
 
console.log('button.constructor: ' + button.constructor); // Not Button 
 
console.log('Typeof button: ' + typeof button); // object 
 
console.log('Typeof button.applyJsonProperties: ' + typeof button.applyJsonProperties); // undefined

鲁道夫Manusadzhyan建议,你可以有元素作为实例的属性,并呼吁那些,例如方法

function Button(tagName) { 
 
    this.button = document.createElement(tagName || 'button'); 
 
} 
 

 
Button.prototype.hide = function() { 
 
    this.button.style.display = 'none'; 
 
} 
 

 
Button.prototype.show = function() { 
 
    this.button.style.display = ''; 
 
} 
 

 
window.onload = function() { 
 
    var button = new Button(); 
 
    button.button.textContent = 'The button'; 
 
    document.body.appendChild(button.button); 
 
    document.getElementById('hideButton').onclick = function(){button.hide()}; 
 
    document.getElementById('showButton').onclick = function(){button.show()}; 
 
};
<input type="button" id="hideButton" value="Hide the button"> 
 
<input type="button" id="showButton" value="Show the button">

+0

谢谢!这非常有帮助! – guest

+0

在这种情况下,你如何确保'var button'是'Button'的一个实例? – guest

1

尝试增加this.button = button;applyJsonProperties引用它作为this.button.style[cssAttribute] = jsonProperties[cssAttribute];和除去return button

function Button(tagName) { 
    var button; 
    if (tagName) button = document.createElement(tagName); 
    else button = document.createElement('div'); 

    button.innerHTML = 'Track Order'; 
    this.button = button; 
} 
Button.prototype.applyJsonProperties = function(jsonProperties){ 
    if(jsonProperties){ 
    for(var cssAttribute in jsonProperties){ 
     this.button.style[cssAttribute] = jsonProperties[cssAttribute]; 
    } 
    } 
} 

var divButton = new Button('div'); 
var props = { "color" : "blue" } 
divButton.applyJsonProperties(props); 
+0

他为什么要这样做?一个好的答案应该解释原始代码中的错误,以及解决方案如何修复它。 – Barmar

+0

我很抱歉地说,但只有几行代码,如果你了解问题属性,你会注意到。无论如何,你可以在这个答案中看到,我已经添加了'this.button = button;'并且在'applyJsonProperties'中引用它为'this.button.style [cssAttribute] = jsonProperties [cssAttribute];' –

+0

把这个解释放在回答,而不是评论。 – Barmar