简体   繁体   中英

In JavaScript, is there a way to print multiple Objects properties using the “this” keyword or a (for, in) LOOP?

var object1= new Object();

object1.name= 'Dana';
object1.age = 18;

var object2= new Object();

object2.name= 'Togo';
object2.age= 21;

var object3= new Object();

object3.name= 'Chris';
object3.age= 29;


object3.rank = function() {    
    console.log( this.name + ' is ' + this.age + ' years of age. '); 

};

object3.rank();

is there a way to just do like...

rank(object1); 

and it prints out

Dana is 18 years of age.

Instead of having to type a method for each object Like one method for all Objects that look similar. I'm not too comfortable with the for(var key in obj) LOOP yet but please let me know if it's possible either way. Thank you so much. I appreciate it. If there is a big gap in my logic, please explain it to me.

There is indeed. Object Oriented Javascript is the answer to what you want to do here.

Here's an example similar to what you've mentioned:

var Person = function (firstName) {
  this.firstName = firstName;
};

Person.prototype.sayHello = function() {
  console.log("Hello, I'm " + this.firstName);
};

var person1 = new Person("Alice");
var person2 = new Person("Bob");

// call the Person sayHello method.
person1.sayHello(); // logs "Hello, I'm Alice"
person2.sayHello(); // logs "Hello, I'm Bob"

Thus the method sayHello here is defined just once - but can be used across each instantiated object!

A superb reference guide can be found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript to help you with the next steps of this. If you have any difficulty using this, feel free to ask.

Hope this helps!

ES6 is now widely in use and has this more maintainable class definition process:

class Person {
   constructor(name) {
    this.name = name;
   }
   sayHello() {
    console.log("Hello, I'm", name);
   }
}

That's it! Now you can invoke using the following:

let person1 = new Person("Alice");
person1.sayHello();

Naturally. Put all your people in an array, and iterate over that array. Easy peasy:

function rank(input) {
  console.log( inputname + ' is ' + inputage + ' years of age. '); 
}

var object1 = ...;
var object2 = ...;
var object3 = ...;

var objects = [object1, object2, object3, ...];

// now call rank() with every entry in the array:
objects.forEach(rank);

But a better approach would be to use objects with a prototype, rather than just plain objects, because that lets you create object instances on which you can call functions:

var Person = function(name, age) {
  this.name = name;
  this.age = age;
};

Person.prototype = {
  rank: function() {
    return this.name + ' is ' + this.age + ' years of age. '; 
  }
}

var people = [
  new Person('a', 1),
  new Person('b', 2),
  new Person('c', 3)
];

people.forEach(function(person) {
  console.log(person.rank());
});

Or, even better, using modern JavaScript classes and syntax:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  },

  rank() {
    return `${this.name} is ${this.age} years of age.`; 
  }
};

let people = [
  new Person('a', 1),
  new Person('b', 2),
  new Person('c', 3)
];

people.forEach(person => console.log(person.rank());

You should try passing the object as a parameter and read the name and age that way.

like as follows:

var object1= new Object();

object1.name= 'Dana';
object1.age = 18;

var object2= new Object();

object2.name= 'Togo';
object2.age= 21;

var object3= new Object();

object3.name= 'Chris';
object3.age= 29;


function rank(obj) {    
 console.log( obj.name + ' is ' + obj.age + ' years of age. '); 

};

rank(object1);

This will put the function in the 'main object' rather than on object3, which would be what you want according to your question:

is there a way to just do like...

rank(object1);

and it prints out

Dana is 18 years of age.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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