Javascript OOP-声明对象的方法

Javascript OOP-声明对象的方法,javascript,oop,Javascript,Oop,这是我能想到的最基本的JS对象示例,它说明了我的问题 问题1. 如何在类中引用函数,以便在其他代码中调用方法?这给了我一个错误 var name1 = new Name(); name1.render(); 问题2. 像这样在线声明函数与使用var getByID=function()…之间有什么区别 对象示例: function Name(user_id, container_id) { this.userID = user_id; this.containerID = c

这是我能想到的最基本的JS对象示例,它说明了我的问题

问题1.

如何在类中引用函数,以便在其他代码中调用方法?这给了我一个错误

  var name1 = new Name();
  name1.render(); 
问题2.

像这样在线声明函数与使用
var getByID=function()…
之间有什么区别

对象示例:

function Name(user_id, container_id) {
  this.userID = user_id;
  this.containerID = container_id;
  this.firstName = null;
  this.lastName = null;

  function getByID(userID) {
    // An ajax call that takes a userID to get a name.
  }

  function setName() {
    // An ajax call to get the name from db.
    name_record = this.getByID(this.userID); ????? this returns an error that getByID is undefined.
    this.firstName = name_record.firstName;
    this.lastName = name_record.lastName;
  }

  function render() {
    $(this.containerID).val(this.firstName + ' ' + this.lastName);
  }
}

问题1: Javascript中的“类”只不过是一个对象。对象具有您可以访问的属性。这些属性可以是变量、函数或其他对象。通过如下方式声明您的函数:

  function render() {
    $(this.containerID).val(this.firstName + ' ' + this.lastName);
  }
您正在function Name()范围内声明一个函数,但该函数不是Name的属性。这只是一种私人方法。有多种方法可以使其成为Name()的一部分,例如:

function Name(user_id, container_id) {
  this.userID = user_id;
  this.containerID = container_id;
  this.firstName = null;
  this.lastName = null;
  this.render = function() {
    console.log('hello world');
  }
}

var name1 = new Name();
name1.render();
问题2:
没有区别。它们只是实现相同结果的两种不同语法。第二种方法(声明变量并定义函数)立即为您提供对函数的引用,但这与第一种方法一样可以实现。

您可以像在第二个问题中那样声明一个对象,这是有效的,因为函数也是一个对象。或其他方式,如:

var Name = {
    render: function() {
    }
}

Name.render();
或与:

所有这些snipet都是有效的对象声明

你的第二个问题可以回答第一个问题。当您声明这样的函数时:

function Name() {
    function render() {
    }
}
var n = new Name();
这就像
render()
是一个私有方法。如果在函数名
n.render()
之外调用,将看到抛出的错误,因为无法识别
render
。但是如果你改成这个

function Name() {
    this.render = function() {
    }
}
。。。然后
n.render()
将像
render()
一样作为公共方法工作。有关
public
private
方法的更多信息,请参阅和

现在,声明函数“串联”或将其设置为变量的区别在于:

function Name() {
}
你可以做:

var n1 = Name();
var n2 = Name();
var nn = Name(); // and so on...
但是:

var n = function Name() {
}
就是说,
n()
将起作用而
Name()
将不起作用。即使
var a=Name()
也会引发异常


关于这个值得一读的话题。我希望它能有所帮助。

回答您的第一个问题:

函数getByID、setName和render是构造函数的本地函数,不能由类对象调用。你必须使用原型继承

例如

function Name(user_id, container_id) {
this.userID = user_id;
this.containerID = container_id;
this.firstName = null;
this.lastName = null;
}

Name.prototype = {

getByID :function(userID) {
  // An ajax call that takes a userID to get a name.
}

setName:function() {
  // An ajax call to get the name from db.
  name_record = this.getByID(this.userID); 
  this.firstName = name_record.firstName;
  this.lastName = name_record.lastName;
}

render:function() {
  $(this.containerID).val(this.firstName + ' ' + this.lastName);
}
};
回答你的第二个问题:

万一 abc()//错误 函数abc(){ }

此函数是在运行时创建的,因此您只能在声明后调用它

但是,

abc();
var abc = function(){
};

是在解析时创建的,因此您可以在声明之前调用它。

您需要将一些参数传递给
Name
container\u id
未定义的
,因此
$(this.containerID)
无法工作。相关:问题2:P.S.不要只说“这给了我一个错误”,告诉我们确切的错误。使用提供的示例代码,这个.userID将是未定义的,因为您没有向构造函数传递任何值。这可能是
上下文的问题。也许你应该检查这个答案,它解释了原型和
this
abc();
var abc = function(){
};