简体   繁体   English

JS中定义函数/方法之间的区别?

[英]differences between defining functions/methods in JS?

Here are 3 ways to address the key-value pair of objects constructed using constructor. 以下是3种方法来解决使用构造函数构造的键值对对象。 What are the differences among these 3 ways in every describable aspects? 在每个可描述的方面,这三种方式有什么不同? (I would even like to enquiry about basic differences between function & method in terms of their functionality, usage, etc.) (我甚至想询问函数和方法在功能,用法等方面的基本差异)

function Person(name,age) {
  this.name = name;
  this.age = age;
}
var bob = new Person("Bob Smith", 30);
var me = new Person('Madhav Devkota', 55);

//===================================================
//A. Simple function
printPerson= function (p) { 
  console.log(p.name);
};
printPerson(bob);  printPerson(me);
//===================================================

//B. Method I
printPerson = function(){
  console.log(this.name) ;  
};
bob.printPerson = printPerson; me.printPerson = printPerson;
bob.printPerson(); me.printPerson();
//=================================================

//C. Method II
this.printPerson = function() {
    console.log(this.name);
};
bob.printPerson();      me.printPerson();

I would also add 我还想补充一下

// 0. No function // 0.没有功能

console.log(bob.name);
console.log(me.name);

It is the most basic way. 这是最基本的方式。 You are doing something with you object properties directly. 您正在使用对象属性直接执行某些操作。

A. Simple function A.简单的功能

You are giving your code a name to improve semantics. 您为代码命名以改进语义。 Now you are describing what your code is intended to do. 现在,您将描述代码的用途。 You can access more properties and combine to create complex result without code repetition. 您可以访问更多属性并组合以创建复杂结果,而无需重复代码。

printPerson = function (p) { 
  console.log(p.name + ' is aged ' + p.age)
}

instead of No function 而不是没有功能

console.log(bob.name + ' is aged ' + bob.age);
console.log(me.name ' is aged ' + me.age);

B. Method I B.方法I.

Now your function is also a property of your object. 现在你的函数也是你对象的属性。 Unlike simple function which works in scope where it is declared, your method it attached to your object and you can pass it around along with it. 与在声明它的范围内工作的简单函数不同,它的方法附加到您的对象上,您可以将它与它一起传递。 When invoked 'this' references the object from which method is invoked. 当调用'this'时,引用从中调用方法的对象。

You can also do a 'nonsense' method like this: 你也可以这样做一个'废话'方法:

printPerson = function(p){
  console.log(p.name) ;  
};
bob.printPerson = printPerson; me.printPerson = printPerson;
bob.printPerson(bob); me.printPerson(me);

C. Method II C.方法II

This one is not quite right. 这个不太对劲。 It doesn't make sense in given context as 'this' is at that moment referencing Window object. 它在给定的上下文中没有意义,因为'this'就是在那个时候引用Window对象。 At then end you are actually calling 'Method I' methods again. 在那时你实际上再次调用'方法I'方法。 Correct way to use it is in constructor function: 正确的使用方法是在构造函数中:

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.printPerson = function() {
    console.log(this.name);
  };
}

Now your objects have . 现在你的对象了。 printPerson () method as soon as they are created. printPerson ()方法一旦创建就立即生效

I could elaborate more if you wish but it's important to notice that function vs method difference is not too relevant at this level of code complexity. 如果你愿意,我可以详细说明,但重要的是要注意函数与方法的差异在这个代码复杂度级别上并不太相关。 When your code gets more complex code organization becomes important. 当您的代码变得更复杂时,代码组织变得非常重要 For 'next level' you should get more familiar with Javascript scoping and object inheritance . 对于“下一级”,您应该更熟悉Javascript 作用域和对象继承

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM