// 比 JSON.parse(JSON.stringify(data)) 還略慢
module.exports = deepCopy;
let jobList = [];
// for test
// 與 JSON.stringify() 對比速度用
function deepCopy(value) {
// debugger;
let dataSet = new DataSet(value);
jobList.push(dataSet);
let index = 0;
while ((dataSet = jobList[index++]) != null) {
// debugger;
let _value = dataSet.originalValue;
if (Array.isArray(_value)) {
for (let i = 0; i < _value.length; i++) {
let v = _value[i];
let ds = new DataSet(v, dataSet, i);
jobList.push(ds);
}
} else if (typeof (_value) == "object") {
for (let k in _value) {
let v = _value[k];
let ds = new DataSet(v, dataSet, k);
jobList.push(ds);
}
} else {
continue;
}
}
//-----------------------
let res;
let d;
while ((d = jobList.pop()) != null) {
res = d;
d.solve();
}
return res.value;
}
//======================================
function DataSet(v, p, k) {
this.parent; // DataSet
this.parentKey;
this.value;
this.originalValue;
this.__construct(v, p, k);
}
(function () {
this.__construct = function (v, p, k) {
this.originalValue = v;
if (p) {
this.parent = p;
this.parentKey = k;
}
this._makeValue();
};
//--------------------------------------
this._makeValue = function () {
if (Array.isArray(this.originalValue)) {
this.value = [];
} else if (typeof (this.originalValue) == "object") {
this.value = {};
} else {
this.value = this.originalValue;
}
};
//--------------------------------------
this.solve = function () {
if (this.parent == null) {
return;
}
let data = this.parent.value;
data[this.parentKey] = this.value;
this._destory();
};
//--------------------------------------
this._destory = function () {
this.parent = undefined;
this.parentKey = undefined;
this.value = undefined;
this.originalValue = undefined;
};
}).call(DataSet.prototype);
2018年11月21日 星期三
2018年11月20日 星期二
worker 與 java 執行續的不同
worker 父執行緒與子執行緒並不會交叉執行
let child = new Worker("./child_1.js");
let it;
child.onmessage = function(e) {
let data = e.data;
console.log(`child ${data.index}`);
// 確保 parent loop 與 child 幾乎同時執行
it.next();
};
function* parentLoop(){
for (var i = 0; i < 100; i++) {
console.log(`parent ${i}`);
}
console.dir("parent finish");
}
function click() {
it = parentLoop();
child.postMessage({});
}
-----------------------------------------------------------------
onmessage = function(oEvent) {
for (var i = 0; i < 100; i++) {
postMessage({index:i});
}
};
let child = new Worker("./child_1.js");
let it;
child.onmessage = function(e) {
let data = e.data;
console.log(`child ${data.index}`);
// 確保 parent loop 與 child 幾乎同時執行
it.next();
};
function* parentLoop(){
for (var i = 0; i < 100; i++) {
console.log(`parent ${i}`);
}
console.dir("parent finish");
}
function click() {
it = parentLoop();
child.postMessage({});
}
-----------------------------------------------------------------
onmessage = function(oEvent) {
for (var i = 0; i < 100; i++) {
postMessage({index:i});
}
};
2018年11月15日 星期四
2018年11月13日 星期二
java 從網址取得 json data 並解析
package test_4;
import org.json.*;
import java.net.*;
import java.io.*;
import static java.lang.System.out;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author xman
*/
public class T_1 {
public static String getURLContent(String urlPath) throws MalformedURLException, IOException {
String content = "";
HttpURLConnection connection = null;
BufferedReader in;
URL url = null;
url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("请输入 URL 地址");
return content;
}
in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String current;
while ((current = in.readLine()) != null) {
content += current;
}
return content;
}
public static void main(String[] args) throws Exception {
String htmlContent = getURLContent("http://localhost:8081/test_1/JSON/t_1.php");
// out.println(htmlContent);
JSONObject j;
j = new JSONObject(htmlContent);
Iterator<String> it = j.keys();;
while (it.hasNext()) {
String key = it.next();
Object o_1 = j.get(key);
out.printf("(%s)=>(%s)\n", key, o_1.toString());
}
}
}
java 閉包 範例
package test_4;
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author xman
*/
public class T_2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int a = 5;
T2_Closure t = (new T2_Closure<Object, Void>() {
@Override
public Void callback(Object in) {
// 引用 a
out.printf("callback(%s)\n", a);
return null;
}
});
t.callback(null);
//------------------------------------
T2_Query q = new T2_Query();
q.query(new T2_Closure<T2_Query, Void>() {
@Override
public Void callback(T2_Query in) {
String res = in.name + " : " + a;
out.println(res);
return null;
}
});
}
}
//--------------------------------------
// 閉包
interface T2_Closure<T, E> {
public E callback(T in);
}
//--------------------------------------
class T2_Query {
String name = "xyz";
void query(T2_Closure closure) {
Thread th = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
out.println(e);
}
closure.callback(T2_Query.this);
});
th.start();
out.println("thread start");
}
}
import static java.lang.System.out;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author xman
*/
public class T_2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int a = 5;
T2_Closure t = (new T2_Closure<Object, Void>() {
@Override
public Void callback(Object in) {
// 引用 a
out.printf("callback(%s)\n", a);
return null;
}
});
t.callback(null);
//------------------------------------
T2_Query q = new T2_Query();
q.query(new T2_Closure<T2_Query, Void>() {
@Override
public Void callback(T2_Query in) {
String res = in.name + " : " + a;
out.println(res);
return null;
}
});
}
}
//--------------------------------------
// 閉包
interface T2_Closure<T, E> {
public E callback(T in);
}
//--------------------------------------
class T2_Query {
String name = "xyz";
void query(T2_Closure closure) {
Thread th = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
out.println(e);
}
closure.callback(T2_Query.this);
});
th.start();
out.println("thread start");
}
}
2018年11月10日 星期六
java callback
/**
*
* @author xman
*/
public class Tt {
String name = "......";
void query(Tool t) {
t.set(this);
}
public static void main(String[] args) {
Tt tt = new Tt();
tt.query(new Tool<Tt>() {
public void set(Tt o) {
out.println(o.name);
}
});
tt.query((o) -> {
// error
out.println(o.name);
});
}
}
interface Tool<T> {
public void set(T in);
}
java 非同步呼叫 callback
public class Ajax_1 {
Callback callback;
String name;
void query(Callback callback) {
this.callback = callback;
// 非同步
Thread t = new Thread(() -> {
// 取得查詢
String name = (new SyncSqlQuery()).query();
Ajax_1.this.name = name;
Ajax_1.this.callback.callback();
});
t.start();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Ajax_1 a = new Ajax_1();
int x = 5;
a.query(() -> {
// 及時命令
out.println(a.name);
});
a.query(new Callback(){
public void callback(){
// 及時命令
out.println(a.name);
}
});
}
}
//-------------------------------------------------------
public interface Callback {
void callback();
}
//-------------------------------------------------------
public class SyncSqlQuery {
public static int id = 0;
String query(){
return "SyncSqlQuery_" + id++;
}
}
//////////////////////////////////////////////////////////////////////////
/**
*
* @author xman
*/
public class Ajax_1 {
String name;
void query(Callback callback) {
Thread t = new Thread(() -> {
// 取得查詢
String name = (new SyncSqlQuery()).query();
Ajax_1.this.name = name;
});
t.start();
try {
t.join();
} catch (InterruptedException ex) {
out.println(ex);
}
callback.callback();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Ajax_1 a = new Ajax_1();
int x = 5;
a.query(() -> {
out.println(a.name);
});
a.query(new Callback(){
public void callback(){
out.println(a.name);
}
});
}
}
java inner-class 閉包
// 運用 innerClass
public class T_4 {
String name;
String getName() {
return name;
}
//----------------------------
class A {
public void setName(String name) {
T_4.this.name = name;
}
}
//----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
T_4.A a = t.new A();
a.setName("Xman");
out.println(t.getName());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 運用 lambda
// 比較方便偷懶
interface Callback {
// callback 內容隨你寫....讚
public void callback(String name);
}
//-------------------------------------------
public class T_4 {
String name;
String getName() {
return name;
}
// 送出一個 callback
// 來設定 name
Callback getCallback() {
// lambda 生成一個 匿名 class implements Callback
return ((name) -> {
T_4.this.name = name;
});
}
// ----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
// 藉由 callback 設定 t
t.getCallback().callback("Xyz");
out.println(t.getName());
}
}
public class T_4 {
String name;
String getName() {
return name;
}
//----------------------------
class A {
public void setName(String name) {
T_4.this.name = name;
}
}
//----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
T_4.A a = t.new A();
a.setName("Xman");
out.println(t.getName());
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
// 運用 lambda
// 比較方便偷懶
interface Callback {
// callback 內容隨你寫....讚
public void callback(String name);
}
//-------------------------------------------
public class T_4 {
String name;
String getName() {
return name;
}
// 送出一個 callback
// 來設定 name
Callback getCallback() {
// lambda 生成一個 匿名 class implements Callback
return ((name) -> {
T_4.this.name = name;
});
}
// ----------------------------
public static void main(String[] args) {
T_4 t = new T_4();
// 藉由 callback 設定 t
t.getCallback().callback("Xyz");
out.println(t.getName());
}
}
2018年11月9日 星期五
java繼承 與變數型態 間的問題
public class T_1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
T_1_B b = new T_1_B();
b.showName();
T_1_A a = b;
a.showName();
try {
a.getName(); // error
} catch (Exception e) {
out.println(e);
}
}
}
//--------------------------------
class T_1_A {
String name = "G";
void showName() {
out.printf("A(%s)\n", name);
}
}
//--------------------------------
class T_1_B extends T_1_A {
void showName() {
String _name = this.getName();
out.printf("B(%s)\n", name);
}
String getName() {
return name;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
T_1_B b = new T_1_B();
b.showName();
T_1_A a = b;
a.showName();
try {
a.getName(); // error
} catch (Exception e) {
out.println(e);
}
}
}
//--------------------------------
class T_1_A {
String name = "G";
void showName() {
out.printf("A(%s)\n", name);
}
}
//--------------------------------
class T_1_B extends T_1_A {
void showName() {
String _name = this.getName();
out.printf("B(%s)\n", name);
}
String getName() {
return name;
}
}
2018年11月8日 星期四
各語言繼承的不同
java 與眾不同
public class T_8 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
T8_A a;
T8_B b;
a = new T8_B();
out.println(a.getName());
b = (T8_B)a;
out.println(b.getName());
}
}
class T8_A{
String name = "A";
public String getName(){
return name;
}
}
class T8_B extends T8_A{
String name = "B";
}
// 答案都是 A
----------------------------------------------------------------------
<?php
class A {
public $name = "A";
public function getName() {
return $this->name;
}
}
class B extends A {
public $name = "B";
}
$b = new B();
printf("%s", $b->getName());
// 答案是 B
----------------------------------------------------------------------
function A(){
this.name = "A";
}
(function(){
this.getName = function(){
return this.name;
};
}).call(A.prototype);
//-------------------------------
function B(){
A.call(this);
this.name = "B";
}
B.prototype = Object.create(A.prototype);
//-------------------------------
console.log((new B()).getName());
// 答案是 B
----------------------------------------------------------------------
class A:
def __init__(self):
self.name = "A"
def getName(self):
return self.name;
class B(A):
def __init__(self):
self.name = "B"
b = B()
print("%s"%b.getName())
// 答案是 B
----------------------------------------------------------------------
public class T_8 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
T8_A a;
T8_B b;
a = new T8_B();
out.println(a.getName());
b = (T8_B)a;
out.println(b.getName());
}
}
class T8_A{
String name = "A";
public String getName(){
return name;
}
}
class T8_B extends T8_A{
String name = "B";
}
// 答案都是 A
----------------------------------------------------------------------
<?php
class A {
public $name = "A";
public function getName() {
return $this->name;
}
}
class B extends A {
public $name = "B";
}
$b = new B();
printf("%s", $b->getName());
// 答案是 B
----------------------------------------------------------------------
function A(){
this.name = "A";
}
(function(){
this.getName = function(){
return this.name;
};
}).call(A.prototype);
//-------------------------------
function B(){
A.call(this);
this.name = "B";
}
B.prototype = Object.create(A.prototype);
//-------------------------------
console.log((new B()).getName());
// 答案是 B
----------------------------------------------------------------------
class A:
def __init__(self):
self.name = "A"
def getName(self):
return self.name;
class B(A):
def __init__(self):
self.name = "B"
b = B()
print("%s"%b.getName())
// 答案是 B
----------------------------------------------------------------------
2018年11月6日 星期二
java 犯行
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test_4;
import static java.lang.System.out;
class T_4_A {
String name = "A";
public T_4_A() {
}
public T_4_A(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class T_4_B extends T_4_A {
public String name = "...B";
public T_4_B() {
super("B");
}
public T_4_B(String name) {
super(name);
}
}
class T_4_C extends T_4_B {
public String name = "...C";
public T_4_C() {
super("C");
}
public T_4_C(String name) {
super(name);
}
}
//==============================================================================
class T_4_Bag<T> {
public int set_index = 0;
public int get_index = 0;
public T[] content;
T_4_Bag() {
this.content = (T[]) (new Object[10]);
}
void add(T t) {
if (set_index == (content.length - 1)) {
throw new Error("bag full");
}
content[set_index++] = t;
}
Boolean next() {
if (get_index < set_index && content[get_index] != null) {
return true;
}
return false;
}
T result() {
return content[get_index++];
}
}
//==============================================================================
/**
*
* @author xman
*/
public class T_4 {
public static void test_1() {
T_4_Bag<T_4_A> bag = new T_4_Bag<>();
bag.add(new T_4_A());
T_4_Bag<? super T_4_B> bag_1 = bag;
// bag_1.add(new T_4_A()); // error
bag_1.add(new T_4_B());
while (bag_1.next()) {
T_4_A o = (T_4_A) (bag_1.result());
out.printf("(%s)=>(%s)\n", o.name, o.getName());
}
}
//--------------------------------------
public static void test_2() {
T_4_Bag<T_4_B> bag = new T_4_Bag<>();
bag.add(new T_4_B());
T_4_Bag<? super T_4_B> bag_1 = bag;
bag_1.add(new T_4_B());
bag_1.add(new T_4_C());
while (bag_1.next()) {
T_4_B o = (T_4_B) (bag_1.result());
out.printf("(%s)=>(%s)\n", o.name, o.getName());
}
}
//--------------------------------------
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// test_1();
test_2();
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test_4;
import static java.lang.System.out;
class T_4_A {
String name = "A";
public T_4_A() {
}
public T_4_A(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class T_4_B extends T_4_A {
public String name = "...B";
public T_4_B() {
super("B");
}
public T_4_B(String name) {
super(name);
}
}
class T_4_C extends T_4_B {
public String name = "...C";
public T_4_C() {
super("C");
}
public T_4_C(String name) {
super(name);
}
}
//==============================================================================
class T_4_Bag<T> {
public int set_index = 0;
public int get_index = 0;
public T[] content;
T_4_Bag() {
this.content = (T[]) (new Object[10]);
}
void add(T t) {
if (set_index == (content.length - 1)) {
throw new Error("bag full");
}
content[set_index++] = t;
}
Boolean next() {
if (get_index < set_index && content[get_index] != null) {
return true;
}
return false;
}
T result() {
return content[get_index++];
}
}
//==============================================================================
/**
*
* @author xman
*/
public class T_4 {
public static void test_1() {
T_4_Bag<T_4_A> bag = new T_4_Bag<>();
bag.add(new T_4_A());
T_4_Bag<? super T_4_B> bag_1 = bag;
// bag_1.add(new T_4_A()); // error
bag_1.add(new T_4_B());
while (bag_1.next()) {
T_4_A o = (T_4_A) (bag_1.result());
out.printf("(%s)=>(%s)\n", o.name, o.getName());
}
}
//--------------------------------------
public static void test_2() {
T_4_Bag<T_4_B> bag = new T_4_Bag<>();
bag.add(new T_4_B());
T_4_Bag<? super T_4_B> bag_1 = bag;
bag_1.add(new T_4_B());
bag_1.add(new T_4_C());
while (bag_1.next()) {
T_4_B o = (T_4_B) (bag_1.result());
out.printf("(%s)=>(%s)\n", o.name, o.getName());
}
}
//--------------------------------------
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// test_1();
test_2();
}
}
訂閱:
文章 (Atom)