javascript:函数和对象...? [英] javascript : function and object...?

查看:95
本文介绍了javascript:函数和对象...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以将一个函数作为一个对象吗?例如:

 函数提示(txt){
this.content = txt;
this.shown = false;

和:

  var tip = new Tip(elem.attr('title')); 

我的问题:


  1. 对于一个对象,你可以为 new 调用一个对象吗?

  2. 使用this成为可能,因为我们使用作为对象的函数


解决方案

您正在寻找 构造函数 概念。 所有JavaScript中的函数是对象,可用于创建对象:

 函数make_person(名字,姓氏,年龄){
person = {};
person.firstname = firstname;
person.lastname = lastname;
person.age = age;
return person;
}
make_person(Joe,Smith,23);
// {firstname:Joe,lastname:Smith,age:23}



<但是,为了创建特定类型的新对象(也就是说,继承原型,构造函数等),函数可以引用 this 如果使用 new 运算符调用它,那么它将返回一个对象,其中包含所有在在这种情况下,这个这个引用了我们创建的新对象。

 函数make_person_object(firstname,lastname,age){
this.firstname = firstname;
this.lastname = lastname;
this.age =年龄;
//注意,我们没有包含返回语句
}

注意 make_person make_person_object 之间的关键区别在于调用 new make_person()(而不是简单的 make_person())不会做任何不同的事情......两者都会产生同一个对象。调用 make_person_object()时,如果没有 new 操作符,将会定义您的 this 这个对象(如果您在浏览器中操作,通常 window )的$ c>属性。



因此:

  var Joe = make_person_object(Joe,Smith ,23); 
console.log(Joe); //未定义
console.log(window.firstname)//Joe(oops)

var John = new make_person_object(John,Smith,45);
console.log(John); // {firstname:John,姓氏:Smith,年龄:45}

正如@RobG所指出的,这种做事方式创建了对每个Person的 make_person_object 原型属性的引用我们创造。这使得我们可以在事后添加方法和属性:

  //假设
之前的所有内容都是make_person_object .prototype.full_name =N / A;
make_person_object.prototype.greet = function(){
console.log(Hello!I'm,this.full_name,Call me,this.firstname);
};
John.full_name //N / A
John.full_name =John Smith;;
make_person_object.full_name //仍然是N / A
John.greet(); //你好!我是约翰史密斯给我打电话约翰

约定有构造函数像 make_person_object 是大写,单数化和名义(因为缺少更好的术语) - 因此我们将有一个 Person 构造函数,而不是一个 make_person_object ,它可能被误认为是一个普通的函数。




Can you call a function as an object? For example:

function Tip(txt){      
    this.content = txt;  
    this.shown = false;  
}

And:

var tip = new Tip(elem.attr('title'));

My questions:

  1. Can you call new for a function, as for an object?
  2. The use of "this" is made possible, because we use that function as an object?

解决方案

You are looking for the constructor concept.

All functions in JavaScript are objects and can be used to create objects:

function make_person(firstname, lastname, age) {
    person = {};
    person.firstname = firstname;
    person.lastname = lastname;
    person.age = age;
    return person;
}
make_person("Joe", "Smith", 23);
// {firstname: "Joe", lastname: "Smith", age: 23}

However, in order to create new objects of a particular type (that is to say, that inherit a prototype, have a constructor, etc), a function can reference this and if it is called with the new operator then it will return an object with all of the attributes that are defined on this in the function - this in such cases references the new object we are creating.

function make_person_object(firstname, lastname, age) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    // Note, we did not include a return statement
}

The key difference to note between make_person and make_person_object is that calling new make_person() (as opposed to simply make_person()) will not do anything different ... both will produce the same object. Calling make_person_object() without the new operator however, will define your this attributes on the current this object (generally window if you are operating in the browser.)

Thus:

var Joe = make_person_object("Joe", "Smith", 23);
console.log(Joe); // undefined
console.log(window.firstname) // "Joe" (oops)

var John = new make_person_object("John", "Smith", 45);
console.log(John); // {firstname: "John", lastname: "Smith", age: 45}

Also, as @RobG points out, this way of doing things creates a reference to the prototype property of make_person_object on each "Person" we create. This enables us to add methods and attributes to persons after the fact:

 // Assuming all that came before
make_person_object.prototype.full_name = "N/A";
make_person_object.prototype.greet = function(){ 
    console.log("Hello! I'm", this.full_name, "Call me", this.firstname); 
};
John.full_name // "N/A"
John.full_name = "John Smith"; 
make_person_object.full_name // Still "N/A"
John.greet(); // "Hello! I'm John Smith Call me John"

Convention has it that constructor functions like make_person_object are capitalized, singularized and "nouned" (for lack of a better term) -- thus we would have a Person constructor, rather than a make_person_object which might be mistaken for an ordinary function.

See also:

这篇关于javascript:函数和对象...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆