我正在学习 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 对象?我认为添加sayHi到Object.prototype不是一个好的解决方案,因为它会 …
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) cleardb ×1
ecmascript-5 ×1
heroku ×1
javascript ×1
mysql ×1
new-operator ×1
object ×1
prototype ×1
spring-boot ×1
utf-8 ×1