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

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
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值