Skip to content

Commit

Permalink
feat:修改属性说明
Browse files Browse the repository at this point in the history
  • Loading branch information
oylx committed Nov 8, 2021
1 parent 1791392 commit 00d6563
Show file tree
Hide file tree
Showing 5 changed files with 1,661 additions and 42 deletions.
27 changes: 27 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "1",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"args": [
"${workspaceFolder}/js/Attributes.js"
]
},
{
"name": "调试 Node.js 程序 - args",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/js/Attributes.js"
}
]
}
44 changes: 37 additions & 7 deletions advanceJs/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class Human {
this.name = options.name
this.age = options.age
}
eat() {}
drink() {}
poon() {}
eat () { }
drink () { }
poon () { }
}
class Solider extends Human {
constructor(options) {
Expand All @@ -14,7 +14,7 @@ class Solider extends Human {
this.id = options.id
this.兵种 = "美国大兵" //公有属性不能以非函数形势存在,只能放在私有属性种
}
攻击() {}
攻击 () { }
}

var solider = new Solider({ name: "oylx", age: 28, 战斗力: 100, id: 20 })
Expand All @@ -24,13 +24,43 @@ class Animal {
constructor() {
this._type = "dog"
}
static eat() {
static eat () {
console.log("eat")
}
get type() {
get type () {
return this._type
}
set type(val) {
set type (val) {
this._type = val
}
}

class Parent {
constructor(firstName, lastName, data) {
this.firstName = firstName
this.lastName = lastName
this.data = data
}

fullName () {
return `${this.firstName} ${this.lastName}`
}
}

class Child extends Parent {
constructor(firstName, lastName, age) {
super(firstName, lastName)
this.age = age
}

fullName () {
return `${super.fullName()} (${this.age})`
}
}

let child = new Child('a', 'b', 'c')
for (let key in child) {
console.log(key, ':', child[key]);
}
console.log(Object.keys(child));

115 changes: 81 additions & 34 deletions advanceJs/curry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
// function abc(x,y,z){
// return [x,y,z]
// }
// var _curry = curry(abc);
// var x = _curry(1)(2)(3);
// const _curry = curry(abc);
// const x = _curry(1)(2)(3);
// console.log(abc.length)

// function buyFriut(){
Expand All @@ -21,7 +21,7 @@
// }, 10000);
// })
// }
// var result = await buyFriut();
// const result = await buyFriut();
// promise.then(()=>{
// console.log('success')
// },()=>{
Expand All @@ -39,7 +39,7 @@
// return this
// }
// }
// var promise = new Promise1((resolve,reject)=>{
// const promise = new Promise1((resolve,reject)=>{
// setTimeout(()=>{
// resolve(101)
// },1000)
Expand All @@ -54,11 +54,10 @@
function abc(a,b,c){
return [a,b,c];
}
var curried = curry(abc);
// function curry(func,fixedParams){
// if(!Array.isArray(fixedParams)){fixedParams=[]}
// return function(){
// var newParams = [].slice.call(arguments);
// const newParams = [].slice.call(arguments);
// if(fixedParams.length+newParams.length<func.length){
// return curry(func,fixedParams.concat(newParams));
// }else{
Expand All @@ -67,34 +66,8 @@ var curried = curry(abc);

// }
// }
console.log(curried(1)(2)(3))

// function curry(fn, args) {
// var length = fn.length;
// var args = args || [];
// return function() {
// var newArgs = args.concat(Array.from(arguments));
// if(newArgs.length < length) {
// return curry.call(this, fn, newArgs);
// } else {
// return fn.apply(this, newArgs)
// }
// }
// }
function curry(fn, args) {
let length = fn.length;
args = args || [];
return function () {
var newArgs = args.concat(Array.from(arguments));
if(newArgs.length<length){
return curry.call(this,fn,newArgs)
}else{
return fn.apply(this,newArgs)
}
}
}

function curry(fn, args) {
function curry_old(fn, args) {
let length = fn.length
args = args || []
return function () {
Expand All @@ -105,4 +78,78 @@ function curry(fn, args) {
return fn.apply(this,newArgs)
}
}
}
}

{

// 第二版
function sub_curry(fn) {
const args = [].slice.call(arguments, 1);
return function() {
return fn.apply(this, args.concat([].slice.call(arguments)));
};
}

function curry(fn, length) {

length = length || fn.length;

const slice = Array.prototype.slice;

return function() {
if (arguments.length < length) {
const combined = [fn].concat(slice.call(arguments));
return curry(sub_curry.apply(this, combined), length - arguments.length);
} else {
return fn.apply(this, arguments);
}
};
}
const x = function(a, b, c) {
return [a, b, c];
}
const fn = curry(x);

fn("a", "b", "c") // ["a", "b", "c"]
fn("a", "b")("c") // ["a", "b", "c"]
fn("a")("b")("c") // ["a", "b", "c"]
fn("a")("b", "c") // ["a", "b", "c"]

}

{
function sub_curry(fn){
return function(){
return fn()
}
}

function curry(fn, length){
length = length || 4;
return function(){
if (length > 1) {
return curry(sub_curry(fn), --length)
}
else {
return fn()
}
}
}

const fn0 = function(){
console.log(1)
}

const fn1 = curry(fn0)

fn1()()()() // 1

}

{
let fn = () =>a => b => c => a+ b+ c
let res = fn()
let x = res(1)(2)(3)
console.log(x);
}

75 changes: 74 additions & 1 deletion js/Attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
(1)value,value是该属性的属性值,默认为undefined。
(2)writable,writable是一个布尔值,表示属性值(value)是否可改变(即是否可写),默认为true。
(3)enumerable,enumerable是一个布尔值,表示该属性是否可遍历,默认为true。如果设为false,会使得某些操作(比如for...in循环、Object.keys())跳过该属性。
(4)configurable,configurable是一个布尔值,表示可配置性,默认为true。如果设为false,将阻止某些操作改写该属性,比如无法删除该属性,也不得改变该属性的属性描述对象(value属性除外)。也就是说,configurable属性控制了属性描述对象的可写性。
(4)configurable,configurable是一个布尔值,表示可配置性,1.目标属性是否可以使用delete删除;2.目标属性是否可以再次设置特性。默认为true。如果设为false,将阻止某些操作改写该属性,比如无法删除该属性,也不得改变该属性的属性描述对象(value属性除外)。也就是说,configurable属性控制了属性描述对象的可写性。
(5)get,get是一个函数,表示该属性的取值函数(getter),默认为undefined。
(6)set,set是一个函数,表示该属性的存值函数(setter),默认为undefined。
*/
Expand Down Expand Up @@ -269,8 +269,81 @@ function fn10 () {
Object.isExtensible(obj) // false
}

// 对象属性的枚举
// 1.for ... in
// 循环可以遍历对象中所有可枚举的对象属性(包括对象自有属性和继承的属性)。注意:会因为各个浏览器不同导致对象属性遍历的顺序与当初构建时的顺序不一致
// 使用 for...in迭代,遍历出【自身】以及【原型链】上的【可枚举】的属性,通过hasOwnProperty进行筛选能遍历出自身可枚举属性
// for (prop in obj) {
// if (!obj.hasOwnProperty(prop)) continue; // 跳过继承属性
// }
// 2.Object.keys()
// 直接遍历出的自身可枚举属性组成的数组
// 3.Object.getOwnPropertyNames()
// 可以访问自身可枚举属性与不可枚举属性
// 4.for ... of
{
function Person () {
this.name = 'fyflying'
}
Person.prototype = {
hobby: 'coding'
}
var person = new Person()
Object.defineProperty(person, 'sex', {
value: 'female'
})
for (var item in person) {
console.log(item + ':' + person[item])
}
// name:fyflying hobby:coding
Object.keys(person) // ["name"]
JSON.stringify(person) //"{"name":"fyflying"}"

}

{
var obj = {}
//第一种情况:writable设置为false,不能重写。
Object.defineProperty(obj, "newKey", {
value: "hello",
writable: false
});
//更改newKey的值
obj.newKey = "change value";
console.log(obj.newKey); //hello

//第二种情况:writable设置为true,可以重写
Object.defineProperty(obj, "newKey", {
value: "hello",
writable: true
});
//更改newKey的值
obj.newKey = "change value";
console.log(obj.newKey); //change value
}
{
var obj = {}
//第一种情况:enumerable设置为false,不能被枚举。
Object.defineProperty(obj, "newKey", {
value: "hello",
});

//枚举对象的属性
for (var attr in obj) {
console.log(attr);
}
//第二种情况:enumerable设置为true,可以被枚举。
Object.defineProperty(obj, "newKey", {
value: "hello",
writable: false,
enumerable: true
});

//枚举对象的属性
for (var attr in obj) {
console.log(attr); //newKey
}
}



Expand Down
Loading

0 comments on commit 00d6563

Please sign in to comment.