如何在JavaScript中使用函数构建对象
我不认为这是一个基于意见的问题,因为我要求 “适当的”技巧,而不是“最好的”技巧或“你最喜欢的” 技术。以下选项有客观的权衡,我很高兴 想知道处理原型的行业标准方法是什么 定义和函数定义是 假设我有很多如何在JavaScript中使用函数构建对象,javascript,jquery,Javascript,Jquery,我不认为这是一个基于意见的问题,因为我要求 “适当的”技巧,而不是“最好的”技巧或“你最喜欢的” 技术。以下选项有客观的权衡,我很高兴 想知道处理原型的行业标准方法是什么 定义和函数定义是 假设我有很多apple对象: var newApple = function(){ return { id: new guid(), color: red } } var apple = newApple(); 我经常吃这些苹果,并称之为: appleManager.e
apple
对象:
var newApple = function(){
return {
id: new guid(),
color: red
}
}
var apple = newApple();
我经常吃这些苹果,并称之为:
appleManager.eatApple(apple, fast)
但我想这样做:
var newApple = function(){
return {
id: new guid(),
color: red,
eat: function(speed){
// a bunch of logic here
}
}
然后打电话
apple.eat(fast);
但在我的脑海中,我想到的是所有的函数
都会占据空间,而以前只有一个函数实例
我还想知道,如果将对象上的eat
定义为eatApple(this,speed)
,是否会是一个更合适的选项-这样函数就位于一个位置,我们只需从对象引用它
关于javascript对象上行为特定功能的定义,有哪些选项和客观权衡?这是一个使用@Potter提到的原型链的例子
function Apple(){
this.id = new guid();
this.color = 'red';
}
Apple.prototype = {
eat:function(speed) {
//do something
}
}
var apple = new Apple();
apple.eat('fast');
console.log(apple.color);//prints 'red'
Apple
函数是一个构造函数。任何附加到这个
指针并放入原型中的东西都将被每个实例“继承”
在JavaScript中,您可以使用原型链模拟经典继承
function Fruit(color) {
this.color = color;
}
Fruit.prototype = {
eat:function() {
console.log('Eating some fruit.');
}
};
function Apple(color) {
Fruit.call(this, color);//Call the parent constructor function
this.isOnATree = true; //Add some new properties
}
Apple.prototype = Object.create(Fruit); //Inherit all parent methods
Apple.prototype.eat = function() { //Override parent eat method
console.log('Eating a' + this.color + ' apple');
};
var apple = new Apple('red');
apple.eat(); //Eating a red apple.
另外,我建议。这里有一个使用@Potter提到的原型链的例子
function Apple(){
this.id = new guid();
this.color = 'red';
}
Apple.prototype = {
eat:function(speed) {
//do something
}
}
var apple = new Apple();
apple.eat('fast');
console.log(apple.color);//prints 'red'
Apple
函数是一个构造函数。任何附加到这个
指针并放入原型中的东西都将被每个实例“继承”
在JavaScript中,您可以使用原型链模拟经典继承
function Fruit(color) {
this.color = color;
}
Fruit.prototype = {
eat:function() {
console.log('Eating some fruit.');
}
};
function Apple(color) {
Fruit.call(this, color);//Call the parent constructor function
this.isOnATree = true; //Add some new properties
}
Apple.prototype = Object.create(Fruit); //Inherit all parent methods
Apple.prototype.eat = function() { //Override parent eat method
console.log('Eating a' + this.color + ' apple');
};
var apple = new Apple('red');
apple.eat(); //Eating a red apple.
另外,我建议。您可以在prototype对象上定义函数。这样,您的函数将被放置在一个中心位置,并且不会消耗更多内存
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
您可能可以在prototype对象上定义函数。这样,您的函数将被放置在一个中心位置,并且不会消耗更多内存
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
Person.prototype.name = function() {
return this.firstName + " " + this.lastName;
};
创建和使用对象方法
对象方法是存储为属性值的函数定义
个人变量={
名字:“约翰”,
姓:“Doe”,
身份证号码:5566,
全名:函数(){
返回this.firstName+“”+this.lastName;
}
};
document.getElementById(“demo”).innerHTML=person.fullName();
也许你可以使用这个脚本。
创建和使用对象方法
对象方法是存储为属性值的函数定义
个人变量={
名字:“约翰”,
姓:“Doe”,
身份证号码:5566,
全名:函数(){
返回this.firstName+“”+this.lastName;
}
};
document.getElementById(“demo”).innerHTML=person.fullName();
也许你可以使用这个脚本。
您需要研究的是原型继承。您将在一个名为prototype的对象上放置Apple对象的任何实例方法。新的es6类语法使这变得非常简洁和容易。您需要研究的是原型继承。您将在一个名为prototype的对象上放置Apple对象的任何实例方法。新的es6类语法使这变得非常干净和简单。