JavaScript的null和undefined,两个等号三个等号

本文探讨了如何检查模型配置是否支持特定输入尺寸,以 Mobilenet V1 和 V2 为例,并展示了相关函数的使用。同时,提供了一个ModelConfig接口和验证函数,以适配不同分辨率的模型加载需求。
const benchmarks = {
  'mobilenet_v2': {
    type: 'GraphModel',
    load: async () => {
      const url =
          'https://storage.googleapis.com/learnjs-data/mobilenet_v2_100_fused/model.json';
      return tf.loadGraphModel(url);
    },
    predictFunc: () => {
      const input = tf.randomNormal([1, 224, 224, 3]);
      return model => model.predict(input);
    }
  },
  'blazeface': {
    type: 'GraphModel',
    supportedInput: [128],
    load: async () => {
      const url =
          'https://tfhub.dev/tensorflow/tfjs-model/blazeface/1/default/1';
      return tf.loadGraphModel(url, {fromTFHub: true});
    },
    predictFunc: (inputResolution = 128) => {
      const input = tf.randomNormal([1, inputResolution, inputResolution, 3]);
      return model => {
        return model.predict(input);
      };
    },
  },
}

function isInputSizeSupported(model) {
    const benchmark = benchmarks[model];
    if (typeof benchmark['supportedInput'] !== 'undefined') {
      return true;
    }
    // Model doesn't support input size.
    return false;
  }

  function isInputSizeSupportedNull(model) {
    const benchmark = benchmarks[model];
    if (benchmark['supportedInput'] != null) {
      return true;
    }
    // Model doesn't support input size.
    return false;
  }

  function isInputSizeSupportedNull2(model) {
    const benchmark = benchmarks[model];
    if (benchmark['supportedInput'] !== null) {
      return true;
    }
    // Model doesn't support input size.
    return false;
  }

  function isInputSizeSupportedTypeofNull(model) {
    const benchmark = benchmarks[model];
    if (typeof benchmark['supportedInput'] != null) {
      return true;
    }
    // Model doesn't support input size.
    return false;
  }

  console.log("use undefined="+ isInputSizeSupported('mobilenet_v2'));
  console.log("use undefined="+ isInputSizeSupported('blazeface'));

  console.log("use null="+ isInputSizeSupportedNull('mobilenet_v2'));
  console.log("use null="+ isInputSizeSupportedNull('blazeface'));

  console.log("use null=="+ isInputSizeSupportedNull2('mobilenet_v2'));
  console.log("use null=="+ isInputSizeSupportedNull2('blazeface'));
  
  console.log("use typeof null="+ isInputSizeSupportedTypeofNull('mobilenet_v2'));
  console.log("use typeof null="+ isInputSizeSupportedTypeofNull('blazeface'));

答案:

use undefined=false
use undefined=true
use null=false
use null=true
use null==true
use null==true
use typeof null=true
use typeof null=true

interface ModelConfig {
  architecture: string;
  inputResolution: number;
  modelUrl?: string;
}

// The default configuration for loading MobileNetV1 based PoseNet.
//
// (And for references, the default configuration for loading ResNet
// based PoseNet is also included).
//
// ```
// const RESNET_CONFIG = {
//   architecture: 'ResNet50',
//   outputStride: 32,
//   quantBytes: 2,
// } as ModelConfig;
// ```
const MOBILENET_V1_CONFIG: ModelConfig = {
  architecture: 'MobileNetV1',
  inputResolution: 257,
} as ModelConfig;

let MOBILENET_V2_CONFIG: ModelConfig = {
  inputResolution: 157,
} as ModelConfig;

function validateModelConfig(config: ModelConfig) {
  config = config || MOBILENET_V1_CONFIG;
  return config;
}

function validateModelConfig2(config: ModelConfig) {
  config = {...MOBILENET_V1_CONFIG, ...config};
  return config;
}

console.log(validateModelConfig(MOBILENET_V2_CONFIG));
console.log(validateModelConfig2(MOBILENET_V2_CONFIG));

答案是:

[LOG]: {
  "inputResolution": 157
} 
[LOG]: {
  "architecture": "MobileNetV1",
  "inputResolution": 157
} 
<think>嗯,用户询问JavaScript中双等号(==)等号(===)的区别。根据提供的引用材料,可以清晰梳理出核心差异。 首先需要明确基本机制:双等号会在类型不同时执行强制类型转换,而三等号要求严格类型匹配。引用[1][2]都强调了这点转换行为,比如"5"==5会返回true,但"5"===5返回false。 关于特殊值的处理值得特别注意。引用[3][4]指出几个关键案例: - nullundefined在双等号比较中相等(null==undefined为true),但三等号不相等 - NaN不等于任何值,包括自身(NaN==NaNNaN===NaN都是false) - 正零负零在三等号比较中被视作相等,这点引用[4]的描述一致 引用[2]的结论特别重要:推荐始终使用三等号以避免类型转换导致的意外结果。类似数字0字符串"0"比较这种陷阱在实际开发中很常见。 最后考虑实际应用场景。用户可能遇到对象比较的需求,引用[3]的深拷贝示例说明:即使内容相同的两个对象,使用三等号比较仍返回false。这说明引用类型比较的是内存地址而非内容价值。 在整理回答时需要突出三点:类型转换差异、特殊值处理差异、使用建议。特别要强调三等号更安全可靠的观点,这与所有引用材料的推荐一致。 </think>在JavaScript中,双等号(`==`)等号(`===`)的区别主要在于**类型检查机制****比较规则**,以下是核心差异总结: --- ### **1. 类型转换机制** - **双等号 (`==`)** 若比较值类型不同,会**先隐式转换类型再比较**: ```javascript "5" == 5; // true(字符串转数字) true == 1; // true(布尔值转数字) null == undefined; // true(特殊规则) ``` > 📌 风险:类型转换可能导致意外结果,如 `0 == false` 为 `true`[^2][^4]。 - **三等号 (`===`)** **严格比较类型值**,类型不同直接返回 `false`: ```javascript "5" === 5; // false(字符串 vs 数字) true === 1; // false(布尔值 vs 数字) null === undefined; // false(类型不同) ``` --- ### **2. 特殊值处理** - **`NaN` 的比较** 两者均认为 `NaN` 不等于自身: ```javascript NaN == NaN; // false NaN === NaN; // false(需用 `isNaN()` 判断)[^3][^4] ``` - **`null` `undefined`** 双等号视其为相等,但三等号严格区分: ```javascript null == undefined; // true null === undefined; // false ``` - **`+0` 与 `-0`** 两者均认为 `+0` `-0` 相等: ```javascript +0 === -0; // true[^4] ``` --- ### **3. 对象比较** 两者**均比较引用地址**而非内容: ```javascript const obj1 = { a: 1 }; const obj2 = { a: 1 }; obj1 == obj2; // false(不同内存地址) obj1 === obj2; // false ``` --- ### **✅ 使用建议** 1. **优先使用 `===`** 避免隐式转换引发的意外行为(如 `"" == false` 为 `true`)[^2][^4]。 2. **明确类型转换时用 `==`** 如需兼容 `null` `undefined`(`if (value == null)` 可覆盖两者)。 3. **复杂对象比较** 需手动遍历属性或使用库函数(如 `_.isEqual()`)。 --- ### **典型场景对比** | 表达式 | `==` 结果 | `===` 结果 | 原因 | |--------------------|-----------|------------|--------------------------| | `"0" == false` | `true` | `false` | 字符串转数字后与布尔值比较 | | `[] == ![]` | `true` | `false` | 空数组转数字 `0`,`![]` 转布尔值后为 `false`→`0` | | `" \t\r\n " == 0` | `true` | `false` | 空白字符串转数字为 `0` | > 💡 结论:**`===` 更安全可靠**,可减少因类型转换导致的逻辑错误[^2][^3][^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值