使用关键字 'this' 的 javascript 函数和对象不起作用

标签 javascript function object this

我的问题是关于 javascript 中的函数和对象。我有三个问题,从一个到另一个。在下面的示例中,我尝试在测试中访问 'a' 的值,但我得到了未定义的值。但是我创建了一个新的测试对象,然后我可以访问“a”值并更改它。

//create a function called test
         var test=function() {
           this.a=2
           this.b=3 };
           test.a//undefined
//create a object called test1 using 'new'
test1 = new test();
test1.a//2
//change the value of a in test1
test1.a=4
test1 //Object { a=4, b=3}

在试图找出发生这种情况的原因时,我遇到了这个 javascript functions are objects?另一个问题由此而生。 该 SO 问题的公认解决方案如下

var addn = function func(a) {
  return func.n + a;
};

addn['n'] = 3;
addn(3);

我将“func.n”更改为“this”,但它不再有效

var addn=function func(a) {
 return this.n+a;
};
addn['n']=3;
addn(3); //NaN

用'this'创建匿名函数也没有帮助

    //anonymous function
var addn=function(a) {
     return this.n+a;
    };
    addn['n']=3;
addn(3); //NaN

为什么使用“this”不起作用?

最后一个问题,使用关键字“new”和“createObject”有什么区别。 Douglas Crokford 建议在他的书中使用“CreateObject”,但我不明白为什么。谢谢大家的意见

最佳答案

1。函数是新对象的构造函数

当你调用new FuncName时,该函数作为一个构造函数,里面的this的值指向被构造的对象(而不是函数本身)。当您删除 new 时,this 变为 undefined,退回到全局对象(除非您处于严格模式)。

2。函数也是对象

每个函数都是Function 的一个实例,所以函数本身也是对象并且可以有自己的属性。这些属性无法通过函数体内的 this.propName 访问,只能通过 funcName.propName 访问。那是因为函数内部的 this 从不 函数对象本身(除非你强制它是,使用 bindcall,或 apply)。


我希望以上两个主题能帮助您了解函数的工作原理。至于你的最后一个问题:Crockford 的 createObject 是实现继承的另一种方式,基本上做了 Object.create 的事情。在兼容 ES5 的浏览器中执行。它允许一个对象直接从另一个对象继承,而不需要你手动创建一个新的构造函数,设置它的 prototype 属性(这是一个函数对象的属性的例子),并创建一个实例使用 new。 Crockford 更喜欢这种方式,并表示他不再使用 new 来支持这种方法。


In response to the questions you asked in chat, here is an attempt to explain what functions are and what they do, with examples.

函数可以只是...函数

你调用他们,他们会做一些事情:

function alertThis(what) {
    alert(what)
}
alertThis("alerting something");

你也可以给它们传值,让它们返回值

function timesTwo(num) {
    return num * 2;
}
timesTwo(2); // 4

它们可以被传递并返回任何东西,包括对象...

function createPerson(firstName, lastName) {
    return {
        firstName : firstName,
        lastName : lastName
    }
}
var john = createPerson('John', 'Doe');
john.lastName; // "Doe"

...和其他功能:

function timesN(n) {
    return function(num) {
        return n * num;
    }
}
var timesThree = timesN(3);
timesThree(5); // 15

函数对象

函数可以像任何普通对象一样传递和返回。那是因为它们对象。与任何对象一样,它们可以具有属性:

function countCalls() {
    countCalls.timesCalled++;
}
countCalls.timesCalled = 0;
countCalls();
countCalls();
countCalls.timesCalled; // 2

函数的一个非常重要的默认属性是prototype。这是一个特殊的属性,我们将了解原因。

函数可以充当新对象的构造函数

函数的行为可以像常规 OO 语言中的类构造函数一样。 当使用 new 调用时,它们会创建一个特定“类”的新对象。这个新对象在函数内部称为 this,并自动返回:

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}
var john = new Person('John', 'Doe');
john.firstName; // "John"
john instanceof Person; // true

...除非你故意返回其他东西:

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    var fakePerson = {
        firstName : firstName,
        lastName : lastName
    };
    return fakePerson;
}
var notPerson = new Person('John', 'Doe');
notPerson.firstName; // "John"
notPerson instanceof Person; // false
// Note: the object called 'this' inside the function is created, but
// after the function is called there is no outside reference to it.

构造函数创建的对象知道是谁创建的,并且可以看到它们的prototype属性

回到真人:

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

// Add something to the Person prototype
Person.prototype.sayHi = function() {
    return "hi, I'm " + this.firstName;
}

var john = new Person('John', 'Doe');
john.sayHi(); // "Hi, I'm John"
john.constructor; // Person

对象 john 可以 sayHi() 因为它可以访问其构造函数的 prototype 属性中的所有内容。但是它不能直接看到 Person 的其他属性(只能通过它们自己的 constructor 属性):

function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    Person.timesCalled++;
    // There is no 'this.timesCalled', only Person.timesCalled
}
Person.timesCalled = 0;
var john = new Person('John', 'Doe');
john.timesCalled; // undefined - john cannot be called, Person can
john.constructor.timesCalled; // 1

关于使用关键字 'this' 的 javascript 函数和对象不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200558/

相关文章:

c++ - 将数组传递给函数——指针与引用(C++ 与 C)

Swift:约束泛型参数以用于更受约束的泛型函数

c# - 如何找出鼠标悬停在代码中创建的图片框?

Javascript onclick 事件未在 Firefox 中触发

Javascript - 创建一个对象并为每个循环增加数组中的计数

javascript - 如何根据对属性的引用确定父对象

Javascript函数作为类参数?

javascript - 如何使用 jQuery 创建自己的函数并在另一个函数中调用它?

javascript - 在两个日期内生成随机日期数组的优雅方法

javascript - 如何在 Javascript 中编写一种有效的方法来处理匹配模式?