单个对象

1
2
3
4
5
6
var obj = {
name: 'Zoe',
describe: function() {
return 'Person named ' + this.name;
}
};

使用时

1
2
3
4
obj.name;	// Zoe
obj.name = 'Zhai'; // 赋值
obj.age = 22; // 自动创建
obj.describe(); // Person named Zhai;

in操作符可以用来检测一个属性是否存在

1
2
'age' in obj;	// true
'foo' in obj; // false,不存在的属性为undefined值

delete操作符可以用来删除一个属性

1
2
delete obj.age;
obj.age; //undefined

构造函数

除了作为“真正”的函数和方法,函数还在JavaScript中扮演第三种角色:如果通过new操作符调用,他们会变为构造函数,对象的工厂。构造函数是对其他语言中的类的粗略模拟。约定俗成,构造函数的第一个字母大写。例如:

1
2
3
4
5
6
7
8
9
// 设置实例数据
function Point(x, y) {
this.x = x;
this.y = y;
}
// 方法
Point.prototype.dist = function () {
return Math.sqrt(this.x*this.x + this.y*this.y);
};

  1. 将*.so文件拷贝到app\libs\armeabi文件夹下
  2. 修改build.gradle文件,在buildTypes下添加

    1
    2
    3
    4
    5
    sourceSets {  
    main {
    jniLibs.srcDirs = ['libs']
    }
    }
  3. 在调用处

    1
    2
    3
    4
    5
    6
    public native String  stringFromJNI();           //jni 函数名  
    public native String getFFmpegVersionFromJNI(); //jni函数名
    static {
    System.loadLibrary("ffmpeg"); //加载.so文件
    System.loadLibrary("ffmpeg-jni"); //加载.so文件
    }

另一个方法
*.so文件导入android到app/src/main/jniLibs文件夹下

安装

1
sudo npm install -g supervisor

全局安装supervisor,mac上需要权限

应用

安装完成后,即可使用命令启动应用

1
supervisor app.js

在WebStorm中使用


按如上设置,Node parameters中设置/usr/local/lib/node_modules/supervisor/lib/cli-wrapper.js --exec /usr/local/bin/node --no-restart-on exit

链接数据库

1
2
3
4
5
6
7
8
9
10
var MongoClient = require('mongodb').MongoClient,
assert = require('assert'); // 断言

var url = 'mongodb://localhost:27017/myproject'
MongoClient.connect(url,function(err,db)){
assert.equal(null,err); // 如果err == null 继续执行
console.log('Connected correctly to server');

db.close();
}

插入文档

1
2
3
4
5
6
7
8
9
10
var insertDocument = function(db,callback) {
var collection = db.collection('document');
collection.insert([{a:1},{a:2},{a:3}],function(err,result){
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the document collection");
callback(result);
});
}

可以在连接时增加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
db.close();
});
});

修改文档

1
2
3
4
5
6
7
8
9
10
11
12
var updateDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Update document where a is 2, set b equal to 1
collection.update({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Updated the document with the field a equal to 2");
callback(result);
});
}

也可以在连接时回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
});
});
});

删除

1
2
3
4
5
6
7
8
9
10
11
var removeDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Insert some documents
collection.remove({ a : 3 }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Removed the document with the field a equal to 3");
callback(result);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close();
});
});
});
});

查询

1
2
3
4
5
6
7
8
9
10
11
12
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
assert.equal(2, docs.length);
console.log("Found the following records");
console.dir(docs)
callback(docs);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
findDocuments(db, function() {
db.close();
});
});
});
});
});

Android代码

  1. 获取手机分辨率

    1
    2
    3
    4
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
  2. dp转px

    1
    2
    3
    4
    public static int dip2px(Context context,float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (dpValue *scale + 0.5f);
    }
  3. px转dp

    1
    2
    3
    4
    public static int px2dip(Context context,float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f) - 15;
    }
  4. 不需要Context得到density的方法

    1
    Resources.getSystem().getDisplatMetrics().density;