Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sharpchen committed Feb 7, 2024
1 parent c3b166d commit 62777c2
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Package manager cache cleaning cheat sheet

|package manager|command|reference|
|---|---|---|
|`nuget`|`dotnet nuget locals all --clear`|[Managing the global packages, cache, and temp folders](https://learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders)|
|`pnpm`|`pnpm store prune`|[pnpm prune](https://pnpm.io/cli/prune/)|
|`npm`|`npm cache clean -f`|[npm-cache](https://docs.npmjs.com/cli/v10/commands/npm-cache)|
52 changes: 52 additions & 0 deletions docs/document/Articles/docs/Validity of discard `_` in csharp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Validity of discard `_` in csharp

As a valid identifier in `C#`, there's different scenarios for `_` to behave as a regular reference or a true discard.

## What might broke `_`?

When `_` as a typed variable or member of a type, it's a true identifier

```cs
int _ = 1;
Console.WriteLine(_);
Console.WriteLine(Shared._); // valid
static class Shared
{
public static int _ = 1;
}
```

`_` inside for deconstruction, parameter list and inline `out` can be typed, but they're all valid discards.

```cs
// Typed discards
(int _, var _) = (1, "1");
int.TryParse("1", out var _);
var f = (int _, string _) => Console.WriteLine(_); // error CS0103: The name '_' does not exist in the current context
Console.WriteLine(_); // error CS0103: The name '_' does not exist in the current context
// Untyped discards
_ = 1;
_ = "1";
(_, _) = (1, "1");
int.TryParse("1", out _);
// you can't have a untyped delegate...
Console.WriteLine(_); // error CS0103: The name '_' does not exist in the current context
```

However, untyped discards can be broke if there exists a true `_` identifier.

```cs
int _ = 1;
(_, _) = ("1", 1); // Cannot assign string to int
int.TryParse("1", out _) // Assign to _, it's not a discard now
```

:::tip
That's why we should prefer typed discard if we do need to be cautious about declared `_`
:::

:::info
`C#` is going to make `_` as discards everywhere? See: [Open issues: Breaking changes](/~https://github.com/dotnet/csharplang/issues/7918)
:::
186 changes: 186 additions & 0 deletions docs/document/JavaScript/docs/4. Scope of `this`.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
# Scope of `this`

## In browser or node?

`this` can have different reference depending on the environment.
If you're on **Firefox**, `this` references to `window` object.
If you're on `node.js`, `this` references to `global` object.

:::code-group

```js[Firefox]
console.log(this);
function logThis() {
console.log(this);
}
logThis(); // they're the same
```

```console[node.js]
$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> console.log(this);
<ref *1> Object [global] {
global: [Circular *1],
clearImmediate: [Function: clearImmediate],
setImmediate: [Function: setImmediate] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
clearInterval: [Function: clearInterval],
clearTimeout: [Function: clearTimeout],
setInterval: [Function: setInterval],
setTimeout: [Function: setTimeout] {
[Symbol(nodejs.util.promisify.custom)]: [Getter]
},
queueMicrotask: [Function: queueMicrotask],
structuredClone: [Getter/Setter],
atob: [Getter/Setter],
btoa: [Getter/Setter],
performance: [Getter/Setter],
fetch: [Function: fetch],
crypto: [Getter]
}
undefined
>
```

:::

## Object Literal

`this` references the object using `function` keyword or function literal .
:::warning
However arrow function does not! `this` inside a arrow function reads the closest execution context(when strict mode off).
:::

```js
const person = {
fullName: null,
age: 0,
withFullName: function(fullName) {
this.fullName = fullName;
return this;
},
withAge(age) {
this.age = age;
return this;
},
withGender: (gender) => {
this.gender = gender // `this` is global or window, will eventually throw
return this;
}
}
// no error
person.withFullName('John Smith').withAge(30);
```

```js
const person = {
fullName: null,
age: 0,
withFullName: fullName => {
this.fullName = fullName;
return this;
},
withAge: age => {
this.age = age;
return this;
}
}
person.withFullName('John Smith').withAge(30);
```

## Binding `this` to an object

### `Function.bind`

```ts
/**
* For a given function, creates a bound function that has the same body as the original function.
* The this object of the bound function is associated with the specified object, and has the specified initial parameters.
* @param thisArg An object to which the this keyword can refer inside the new function.
* @param argArray A list of arguments to be passed to the new function.
*/
bind(this: Function, thisArg: any, ...argArray: any[]): any;
```

`Function.bind` re-targets `this` to the the given `thisArgs`

```js
const person = {
fullName: null,
age: 0,
withFullName: function(fullName) {
this.fullName = fullName;
return this;
},
withAge: function(age) {
this.age = age;
return this;
}
}
function withGender(gender) {
this.gender = gender;
return this;
}

const bindWithPerson = withGender.bind(person.withFullName('John Smith').withAge(30), 'male');
console.log(bindWithPerson().gender) // male
```

### `Function.call` and `Function.apply`

```ts
/**
* Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.
* @param thisArg The object to be used as the this object.
* @param argArray A set of arguments to be passed to the function.
*/
apply(this: Function, thisArg: any, argArray?: any): any;

/**
* Calls a method of an object, substituting another object for the current object.
* @param thisArg The object to be used as the current object.
* @param argArray A list of arguments to be passed to the method.
*/
call(this: Function, thisArg: any, ...argArray: any[]): any;
```

Instead of returning a new function using `Function.bind`, use `Function.call`

```js
const personWithGender = withGender.call(person, 'male');
// or
const personWithGender = withGender.apply(person, ['male']);
```

:::tip
`Function.call` is prefered as choice for modern javascript since it takes variadic parameter.
:::

### constructor

Using function as constructor with `new` operator, it creates a new object and auto bound `this` to this new object.

```js
function Person(name, age) {
this.name = name;
this.age = age;
}

const person = new Person('John Smith', 30);
```

## `use strict`

ES5 introduced strict mode that disallow `this` targeting global or window object.
For most web framework, strict mode is enabled by default.

```js
'use strict'

const f = () => {
console.log(this); // undefined
}
```
47 changes: 47 additions & 0 deletions docs/document/Mars3D/docs/Project Setuo/Init Project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Init Project

## Create project

```bash
pnpm create vue@latest
```

## Add mars3d dependencies

Install main lib.

```bash
pnpm add mars3d mars3d-cesium @turf/turf --save
```

```bash
pnpm add mars3d-space --save
```

Install vite plugin

```bash
pnpm add vite-plugin-mars3d --save-dev
```

## Add plugin

```ts{6,10}
// vite.config.ts
import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { mars3dPlugin } from 'vite-plugin-mars3d';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), vueJsx(),mars3dPlugin()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
});
```
Empty file.

0 comments on commit 62777c2

Please sign in to comment.