Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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/delphi/8.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 - Fatal编程技术网

Javascript 如何按照工厂模式在对象中调用方法

Javascript 如何按照工厂模式在对象中调用方法,javascript,Javascript,我想在一个警告消息框中显示姓名、年龄、工作,我该怎么做 function createPerson(name, age, job) { var o = new Object(); o.name = name; o.age = age; o.job = job; o.sayName = function () { alert(this.name); }; return o; } var person1 = createPers

我想在一个警告消息框中显示姓名、年龄、工作,我该怎么做

function createPerson(name, age, job) {
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function () {
        alert(this.name);
    };
    return o;
}
var person1 = createPerson('Nicholas', 29, 'Software Engineer');
var person2 = createPerson('Greg', 27, 'Doctor');

“我想在警报消息框中显示姓名、年龄、工作,我该怎么做?”

使用
.name
.age
.job
访问对象的属性,如下所示:
alert(person1.name+“”+person1.age+“”+person1.job)来自外部。如果希望对象能够使用此警报,则可以如下方式附加该警报:

o.alertInformation=function(){alert(this.name+“”+this.age+“”+this.job);}

function createPerson(name, age, job){
 var o = new Object();
 o.name = name;
 o.age = age;
 o.job = job;
 o.sayName = function(){
  alert(this.name);
 };
 o.alertInformation = function(){ alert(this.name+" "+this.age+" "+this.job); };
 return o;
}

var person1 = createPerson('Nicholas', 29, 'Software Engineer');
var person2 = createPerson('Greg', 27, 'Doctor');

//example of accessing the object properties with dot notation
//alternatively, you could use person1["name"] to access them
alert(person1.name+" "+person1.age+" "+person1.job);

//or if you want to use an internal method on person
person1.alertInformation();

“工厂”模式简介:

通常在函数上调用
new
关键字时使用一种方法。在函数上使用
new
时,它会在函数中创建一个作用域,其中
引用函数对象。在使用
new
调用的函数中使用
this.name
name
附加到对象。使用
new
时,它会将函数对象隐式分配给变量,在下面的示例中,使用
p

function Person(name, age, job){
 this.name = name;
 this.age = age;
 this.job = job;
}

Person.prototype.sayName = function(){
 alert(this.name);
};

Person.prototype.alertInformation = function(){   
 alert(this.name+" "+this.age+" "+this.job);
};

var p = new Person('Nicholas', 29, 'Software Engineer');
p.sayName();
p.alertInformation();

要使它成为一个实际的工厂,请记住它必须涉及到对象的实际创建,而不仅仅是隐式返回它们。要做到这一点,我们需要一个PersonFactory(听起来很奇怪:P)

然后将使用:

var pf = personFactory();

var p = pf.Create('Nicholas', 29, 'Software Engineer');
p.sayName();
p.alertInformation();
这种方法被称为“动态原型”,是已知的最佳方法。
我希望这能帮助…

您需要调用
sayName
方法。使用
person1.sayName()这被称为工厂方法,还有人使用这种模式吗?@CartrightMellisa-是的,这种模式仍然被广泛使用。尽管实现可能略有不同。在javascript中,此模式也称为模块模式,尽管它通常包括在函数上使用
new
关键字,而不是按照所示的方式实际创建对象。@CartrightMellisa-有关创建这些对象的更常见实现的一些详细信息,请参阅编辑。
var pf = personFactory();

var p = pf.Create('Nicholas', 29, 'Software Engineer');
p.sayName();
p.alertInformation();
function createPerson(name, age, job){
this.name=name;
this.age=age;
this.job=job;
if(createPerson._intialized=="undefined"){
    createPerson.prototype.sayPersonInfo=function(){
          alert('i am '+this.name+' , '+this.age+'years old and my job is '+this.job);
}
createPerson._initialized=true;
}
var person1 = createPerson('Nicholas', 29, 'Software Engineer');
var person2 = createPerson('Greg', 27, 'Doctor');
person1.sayPersonInfo();