翻閱古今
2019-03-20 17:15:19
Javascript中如何不調(diào)用構造函數(shù)實例化一個對象?
5 回答

繁華開滿天機
TA貢獻1816條經(jīng)驗 獲得超4個贊
你是說實例化一個類吧,對于用戶自定義的類,
new f(a, b, c)
相當于
// Create a new instance using f's prototype.
var newInstance = Object.create(f.prototype), result;
// Call the function
result = f.call(newInstance, a, b, c),
// If the result is a non-null object, use it, otherwise use the new instance.
result && typeof result === 'object' ? result : newInstance

小唯快跑啊
TA貢獻1863條經(jīng)驗 獲得超2個贊
function createPeople(name,age){
var o = {};
o.name = name;
o.age = o.age;
return o;
}
var people = createPeople('chen',30);
添加回答
舉報
0/150
提交
取消