重写javascript类中的默认get,例如php中的uu get
我正在构建一个javascript库,我希望能够完全像PHP的_get一样 我的库有一个重写javascript类中的默认get,例如php中的uu get,javascript,php,dynamic,getter,Javascript,Php,Dynamic,Getter,我正在构建一个javascript库,我希望能够完全像PHP的_get一样 我的库有一个attributes属性,用于存储每个模型的属性。现在,我被迫使用.get方法获取属性。但是我可以用一个getter来完成它。假设用户扩展了我的模型类 let instance = new User({firstname: 'John', lastname: 'Doe'}); console.log(instance.get('firstname')); // gives me 'John' 我希望能够执行
attributes
属性,用于存储每个模型的属性。现在,我被迫使用.get
方法获取属性。但是我可以用一个getter来完成它。假设用户扩展了我的模型类
let instance = new User({firstname: 'John', lastname: 'Doe'});
console.log(instance.get('firstname')); // gives me 'John'
我希望能够执行instance.firstname
,它将调用.get
方法,并将“firstname”作为参数传递。在PHP中,您可以这样做:
这可能吗
谢谢大家只需在循环中分配属性:
User = function (attrs) {
for (var name in attrs) {
this[name] = attrs[name];
}
}
User.prototype = {
// further methods
}
使用ES6类语法,-我必须承认我不认为这样写有什么意义:
class User {
constructor (attrs) {
for (var name in attrs) {
this[name] = attrs[name];
}
}
// further methods
}
记住:第二种语法与第一种语法完全相同,只是上面加了一些糖 非常简单地在循环中分配属性:
User = function (attrs) {
for (var name in attrs) {
this[name] = attrs[name];
}
}
User.prototype = {
// further methods
}
使用ES6类语法,-我必须承认我不认为这样写有什么意义:
class User {
constructor (attrs) {
for (var name in attrs) {
this[name] = attrs[name];
}
}
// further methods
}
记住:第二种语法与第一种语法完全相同,只是上面加了一些糖 使用ES 2015课程很容易:
class Foo {
constructor () {
this._bar = null;
}
get bar () {
doStuff();
return this._bar;
}
set bar (val) {
doOtherStuff();
this._bar = val;
return this;
}
};
var foo = new Foo();
foo.bar = 3; // calls setter function
console.log(foo.bar); // calls getter function
以下是babel的(简化)输出:
var Foo = function () {
function Foo() {
this._bar = null;
}
_createClass(Foo, [{
key: "bar",
get: function get() {
doStuff();
return this._bar;
},
set: function set(val) {
doOtherStuff();
this._bar = val;
return this;
}
}]);
return Foo;
}();
请注意,这不仅仅适用于类,任何任意对象都可以具有以下特性:
var baz = {
get qux() {
// arbitrary code
},
set qux(val) {
// arbitrary code
}
};
编辑
您想要的是可能的,但仅在本机ES 6环境中,因为无法填充
Proxy
var getter = function(target, property, proxy) {
console.log(`Getting the ${property} property of the obj.`);
return target[property];
};
var setter = function(target, property, value, proxy) {
console.log(`Setting the ${property} property to ${value}.`);
target[property] = value;
};
var emptyObj = {};
var obj = new Proxy(emptyObj, {
get: getter,
set: setter
});
obj.a = 3; // logs 'Setting the a property to 3'
var foo = obj.a; // logs 'Getting the a property of the obj'
这很容易使用ES 2015类:
class Foo {
constructor () {
this._bar = null;
}
get bar () {
doStuff();
return this._bar;
}
set bar (val) {
doOtherStuff();
this._bar = val;
return this;
}
};
var foo = new Foo();
foo.bar = 3; // calls setter function
console.log(foo.bar); // calls getter function
以下是babel的(简化)输出:
var Foo = function () {
function Foo() {
this._bar = null;
}
_createClass(Foo, [{
key: "bar",
get: function get() {
doStuff();
return this._bar;
},
set: function set(val) {
doOtherStuff();
this._bar = val;
return this;
}
}]);
return Foo;
}();
请注意,这不仅仅适用于类,任何任意对象都可以具有以下特性:
var baz = {
get qux() {
// arbitrary code
},
set qux(val) {
// arbitrary code
}
};
编辑
您想要的是可能的,但仅在本机ES 6环境中,因为无法填充
Proxy
var getter = function(target, property, proxy) {
console.log(`Getting the ${property} property of the obj.`);
return target[property];
};
var setter = function(target, property, value, proxy) {
console.log(`Setting the ${property} property to ${value}.`);
target[property] = value;
};
var emptyObj = {};
var obj = new Proxy(emptyObj, {
get: getter,
set: setter
});
obj.a = 3; // logs 'Setting the a property to 3'
var foo = obj.a; // logs 'Getting the a property of the obj'
我见过这样的东西,问题是属性总是变化的。您的建议是,在对象创建时,通过属性创建getter?不确定我是否完全理解,但如果它们是动态的,那么,为什么不呢?属性对象是普通对象;你可以按你想要的方式建造它。现在我明白了。您希望用一个函数容纳多个属性,而我正在考虑一个getter函数,它可以在输出之前转换数据。我将针对您的问题更改我的示例。这是我正在构建的库,如果您想了解我在做什么,实际上我使用找到的模式将我的示例缩短了一点,并添加了类语法。我看到了类似的内容,问题是属性总是会更改。您的建议是,在对象创建时,通过属性创建getter?不确定我是否完全理解,但如果它们是动态的,那么,为什么不呢?属性对象是普通对象;你可以按你想要的方式建造它。现在我明白了。您希望用一个函数容纳多个属性,而我正在考虑一个getter函数,它可以在输出之前转换数据。我将针对您的问题更改我的示例。这是我正在构建的库,如果您想了解我在做什么,实际上我已经使用找到的模式将示例缩短了一点,并添加了类语法。我已经知道所有这些。问题是,属性是动态的。我希望
foo.bar
调用foo.get('bar')
,而foo.baz
调用foo.get('baz')
,在short@NicolasBoisvert更新。这是可能的,但还不是很便携。我已经知道了所有这些。问题是,属性是动态的。我希望foo.bar
调用foo.get('bar')
,而foo.baz
调用foo.get('baz')
,在short@NicolasBoisvert更新。这是可能的,但还不是很便携。