Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Реализация наследования (https://javascript.ru/forum/misc/71010-realizaciya-nasledovaniya.html)

Anna_Medvid 18.10.2017 22:26

Реализация наследования
 
Здравствуйте подскажите как реализуются статические методы в фукциональном, прототипном наследовании и с помощью класов в ES6? и чем отличается статический от приватного методы?
к примеру у меня есть код:
function Animal(name, age, gender, height, weight) {
	let height = height;
	let weight = weight;
	this.name = name;
	this.age = age;
	this.gender = gender;
	this.run = function(){
		console.log('I\'m runing');
	}

	this.getName = function(){
		return this.name;
	};
	this.setName = function(newName){
		this.name = newName;
	}
	this.getAge = function(){
		return this.age;
	};
	this.setAge = function(newAge){
		this.age = newAge;
	}
	function greet(){
		console.log('Hello! I\'m animal');
	}
}

function Mammal (name, age, gender, kind){
	Animal.call(this);
	this.kind = kind;
	this.eat = function(){
		console.log('I\'m runing');
	}
	function greet(){
		console.log(`Hello! I'm mammal. My name ${name}, age ${age}, gender ${gender}, kind ${kind}`);
	}
}


нужно сделать метод greet статичным у Animal и Mammal

ruslan_mart 19.10.2017 00:03

class Animal {
	constructor(name, age, gender, height, weight) {
		this.name = name;
		this.age = age;
		this.gender = gender;
	}
	
	run() {
		console.log('I\'m runing');
	}
	getName() {
		return this.name;
	}
	setName(newName) {
		this.name = newName;
	}
	getAge() {
		return this.age;
	}
	setAge(newAge) {
		this.age = newAge;
	}
	
	static greet() {
		console.log('Hello! I\'m animal');
	}
}

class Mammal extends Animal {
	constructor(name, age, gender, kind) {
		super();

		this.kind = kind;

		//Mammal.greet(name, age, gender, kind);
	}
	
	eat() {
		console.log('I\'m runing');
	}
	
	static greet(name, age, gender, kind) {
		console.log(`Hello! I'm mammal. My name ${name}, age ${age}, gender ${gender}, kind ${kind}`);
	}
}

Anna_Medvid 19.10.2017 11:31

Это ES6, а как реализовать статический метод в функциональном наследовании, и в прототипном наследовании

ruslan_mart 19.10.2017 22:49

Anna_Medvid,

Animal.myMethod = function() {

};


Часовой пояс GMT +3, время: 14:27.