是否可以访问原型变量GLOBALLY

时间:2015-10-03 08:58:24

标签: javascript

    try
    {
        DateTime time = DateTime.Parse(Console.ReadLine());
    }
    catch (FormatException)
    {
        Console.WriteLine("Wrong date and time format!");
    }

这里我想访问变量“name”到我的其他原型。这可能吗?

如果没有,还有其他办法吗?

http://jsfiddle.net/KabeerRifaye/bcxqx1wj/

提前致谢。

2 个答案:

答案 0 :(得分:1)

你应该去做一些JavaScript教程。我推荐TreeHouse或CodeSchool。

无论如何,您的问题有一些可能的选择/解释。

如果您只是想获得返回值,则需要使用return关键字。现在,您的console.log将输出名称。

function Person() {}

Person.prototype.getFullName = function(){
  var name = "John Micheal";
  return name;
}

var p1 = new Person();
console.log(p1.getFullName());

如果您真的想要在功能之间共享价值,则需要使用this关键字。在此示例中,您可以在setName中设置值,并使用getName检索它。

function Person() {}

Person.prototype.getName = function(){
  return this.name
}

Person.prototype.setName = function(newName) {
  this.name = newName
}

var p1 = new Person();

p1.setName('John');
console.log(p1.getName());

这些是您自己学习的难题。我真的建议您通过一些JavaScript教程来学习这些基本概念。

答案 1 :(得分:0)

我更新了你的版本=> http://www.postgresql.org/docs/9.4/static/runtime-config-resource.html

我强烈建议使用更通用的get函数:

Person.prototype.getData = function(prop){
    if (this[prop] !== undefined) {
        return this[prop];
    } else {
        return false;
    }
}

设置功能如下:

Person.prototype.setData = function(prop, data){
    this[prop] = data;
}
相关问题