Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript构造函数中的私有函数不能公开访问_Javascript_Object - Fatal编程技术网

Javascript构造函数中的私有函数不能公开访问

Javascript构造函数中的私有函数不能公开访问,javascript,object,Javascript,Object,我有这个人反对 function person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; function sayName(){ var full=""; full="Hello my name is "+this.firstName + "

我有这个人反对

function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;

    function sayName(){
        var full="";    
        full="Hello my name is "+this.firstName + " "+this.lastName;
    }
}
并制作了这个物体的一个实例

var raul = new person("Raul","Zamora","19","brown")
我不明白为什么sayName函数不起作用。我是这样实施的:

document.getElementById("sayName").innerHTML=raul.sayName();

其中,
sayName
已定义为HTML部件上的id

它不起作用,因为
sayName
函数只在构造函数的作用域中可见(因此是完全无用的)

要使该函数在实例上可用,请使用

person.prototype.sayName = function(){
   var full="Hello my name is "+this.firstName + " "+this.lastName;
   return full; // don't forget to return the string
}

有关更多详细信息,我建议使用MDN中的这篇文章:

您的函数
sayName
不属于您的类,为此,您需要使用
this
propotype

使用此:

function person(first, last, age, eye) {
            this.sayName = function(){
                return "Hello my name is "+this.firstName + " "+this.lastName;
            }
        }
使用
原型

person.prototype.sayName = function(){
     return "Hello my name is "+this.firstName + " "+this.lastName;
}

另外,
sayName
没有返回任何内容。

这里有一个本地函数声明。通过赋值使其成为对象的属性-就像您编写的一样

这将使其成为可从外部访问的方法:

function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;

    this.sayName = function sayName(){
        var full="Hello my name is "+this.firstName + " "+this.lastName;
    };
}
然而,有一些反对意见:

  • 构造函数名称应大写
  • 您的
    sayName
    方法需要
    return
    字符串,否则示例调用将无法工作
  • 将原型用于可在实例(如方法)之间共享的值
所以我们开始

function Person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
Person.prototype.sayName = function sayName(){
    return "Hello my name is "+this.firstName + " "+this.lastName;
};

或者在构造函数内部,
this.sayName=function()…
@Bart在这种情况下,因为它使用
this
,这是个坏主意。为什么这是个坏主意?@Bart我在另一个答案下面解释了它:为每个实例创建函数是没有用的。请看一下我链接到的文档。正如我在回答中所解释的,每个实例都有一个这样做的用例:私有成员。您个人是否更喜欢闭包模式而不是原型模式完全是您的意见,从定义上讲,这不是一个“坏”主意。这种构造为每个实例构建了不同的函数。最好使用原型,因为“更好”是主观的。性能在这里可能并不重要,而且这个模式是唯一允许真正的私有成员的模式,以防您需要它。@巴特,您在这里看到私有成员在哪里?@dystroy,3次编辑之前,他使用的是
firstName
而不是
this.firstName
…我猜您的意思是
person.prototype.sayName
(使用
prototype
)。另外:仅供参考,最好将构造函数名称大写(例如
Person
)。以下内容可能有助于您理解构造函数功能和原型:
var raul = new Person("Raul","Zamora","19","brown");
document.getElementById("sayName").textContent = raul.sayName();