小编ask*_*low的帖子

函数应该附加到对象还是它的原型上?

我正在学习 javascript,我想知道对象内部的函数和原型的概念。

我对概念的理解

据我了解,函数应该附加到对象的原型上以分配更少的内存。

例如(如果我说的是真的),两个版本都会做同样的工作,但第二个版本会分配更少的内存:

var collectionOfObjects = [];
for(var i=0; i<1000; i++){
    collectionOfObjects.push({id: 2, sayHi: function(){console .log('hi')}})
}
//vs
function Foobar(id){
    this.id = id || 0;
}
Foobar.prototype.sayHi = function(){
    console.log('hi');
}
var otherCollection = [];
for(var i=0; i<1000; i++){
    otherCollection.push(new Foobar());
}
Run Code Online (Sandbox Code Playgroud)

//this attaches sayHi function to the object instead of its prototype
var foobar = {
    id: 0,
    sayHi: function(){
        console.log('Hi');
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我不应该使用__proto__,我如何将sayHi函数附加到 foobar 的原型而不是 foobar 对象?我认为添加sayHiObject.prototype不是一个好的解决方案,因为它会 …

javascript prototype object new-operator ecmascript-5

5
推荐指数
1
解决办法
175
查看次数

How to set UTF8 encoding for ClearDB (MySQL) on Heroku

Database I have on heroku doesn't support special characters so I want to set utf8 encoding. When I was working on local version I simply changed config files but I wonder how I can achieve this using Heroku's DB.

This added to connection url doesn't help:

?useUnicode=true&characterEncoding=UTF-8
Run Code Online (Sandbox Code Playgroud)

mysql heroku utf-8 spring-boot cleardb

2
推荐指数
1
解决办法
737
查看次数